<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title>GNUcode.me</title><id>https://gnucode.me/feeds/tags/Scheme API.xml</id><subtitle>Tag: Scheme API</subtitle><updated>2024-05-01T13:21:27Z</updated><link href="https://gnucode.me/feeds/tags/Scheme API.xml" rel="self" /><link href="https://gnucode.me" /><entry><title>Creating New Build Systems</title><id>https://gnucode.me/creating-new-build-systems.html</id><author><name>Mitchell Schmeisser &lt;mitchellschmeisser@librem.one&gt;</name><email>jbranso@dismail.de</email></author><updated>2023-02-24T12:00:00Z</updated><link href="https://gnucode.me/creating-new-build-systems.html" rel="alternate" /><content type="html">&lt;p&gt;In Guix each package must specify a so-called &lt;code&gt;build-system&lt;/code&gt;, which
knows how to bring a package from its inputs to deployment.
Build systems are responsible for setting up the environment and performing
build actions within that environment. The most ubiquitous of these is the
&lt;a href=&quot;https://www.gnu.org/software/automake/manual/html_node/GNU-Build-System.html&quot;&gt;&lt;code&gt;gnu-build-system&lt;/code&gt;&lt;/a&gt;.
Guix builds packages using this build system via the usual
&lt;code&gt;./configure &amp;amp;&amp;amp; make &amp;amp;&amp;amp; make install&lt;/code&gt; process.&lt;/p&gt;&lt;p&gt;Any package can alter its build system by removing some steps or
adding extra ones. This is extremely common and almost every package
makes some adjustment to the build process. A &lt;code&gt;build-system&lt;/code&gt; in Guix
hides away some of the common configuration choices. For example, there is
no need to specify &lt;code&gt;make&lt;/code&gt; or &lt;code&gt;gcc&lt;/code&gt; as native inputs when using the &lt;code&gt;gnu-build-system&lt;/code&gt;,
because they are added implicitly when a package is lowered into a &lt;em&gt;bag&lt;/em&gt;.&lt;/p&gt;&lt;h2&gt;Anatomy of a Guix Build System&lt;/h2&gt;&lt;p&gt;The job of a build system is to compile our &lt;em&gt;packages&lt;/em&gt; into &lt;em&gt;bags&lt;/em&gt;.
Bags are a lower level representation of a package without all the bells and whistles
(Makes sense since we are implementing them here),
the bags are further refined to derivations which are used by the
build daemon to create an isolated environment suitable for our
&lt;em&gt;build phases&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;Below is how guix defines a build system.
It's surprisingly simple with just three items, two of which are for human consumption.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;(define-record-type* &amp;lt;build-system&amp;gt; build-system make-build-system
    build-system?
    (name        build-system-name)         ; symbol
    (description build-system-description)  ; short description
    (lower       build-system-lower))       ; args ... -&amp;gt; bags&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The last field &lt;code&gt;lower&lt;/code&gt; is a function which takes the list of arguments
given in the &lt;code&gt;(package ... (arguments ...))&lt;/code&gt; field.
The keyword arguments that we are allowed to supply when writing the
package are defined by the build-system.&lt;/p&gt;&lt;h1&gt;Code Strata&lt;/h1&gt;&lt;p&gt;Guix builds are implemented in two parts.&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;p&gt;Code that compiles &lt;code&gt;packages-&amp;gt;derivations&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Derivations are the language the Guix Daemon speaks.
They describe everything about how to &lt;em&gt;derive&lt;/em&gt; our package
from the inputs to the environment and all the code on how to drive
the build tools.
This code is run in a poorly defined &amp;quot;user&amp;quot; environment.
Guix produces derivations that actually can be influenced by
undeclared aspects of the environment, like manually installed Guile
packages or code added with the `-L` flag.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;The guix daemon runs the builder code in as isolated and reproducible build environment to produce the package from its inputs.&lt;/p&gt;&lt;p&gt;This code is executed in an explicitly defined build environment
with nothing being introduced from the host.&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Code that runs in the host environment &lt;em&gt;stages&lt;/em&gt; code, which will run in isolation.
This is where G-Expressions really shine.
They provide the syntax to describe this relationship.&lt;/p&gt;&lt;h1&gt;Build Phases&lt;/h1&gt;&lt;p&gt;All programs are built more or less the same way.&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;p&gt;Unpack the source code.&lt;/p&gt;&lt;p&gt;Whether it's tarball or a version controlled repository, the guix daemon must
copy the software's source tree into our build environment.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Patch the shebangs.&lt;/p&gt;&lt;p&gt;Many projects contain scripts written to aid the build process.
In Linux, executable scripts can contain a so-called &lt;em&gt;shebang&lt;/em&gt;,
which contains an absolute path to the program, which is meant to
interpret it: e.g. &lt;code&gt;#!/bin/sh&lt;/code&gt;. Most distributions provide a
POSIX compliant shell interpreter at this location. Guix System does not do this.
Instead, Guix System's &lt;code&gt;sh&lt;/code&gt; is yet another component in the store, so all of these files must
be patched to point to the new location, which is only known at
build time.  We cannot rely on our &lt;code&gt;PATH&lt;/code&gt; to store this information,
because the Linux kernel does not respect such things.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Configure the build.&lt;/p&gt;&lt;p&gt;Whether it is autotools or CMake or ninja, etc., if you are relying
on tools from the host system, then you have a step, which enables the
host system to tell you where to find those tools.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Patch the generated shebangs.&lt;/p&gt;&lt;p&gt;Sometimes the configure phases creates scripts, which run during the build phase,
these often contain references to &lt;code&gt;/bin/sh&lt;/code&gt;, and so guix must patch these scripts as well.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Build!&lt;/p&gt;&lt;p&gt;That's right folks we are off to the races, and the program is building.
Usually this takes the form of a call to &lt;code&gt;make&lt;/code&gt;, with a handful of
flags and last minute definitions, but there are other more &lt;em&gt;exotic&lt;/em&gt; methods.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Test.&lt;/p&gt;&lt;p&gt;Now that guix built our software, we should test it before we sent software
updates to users. This helps the guix project catch and eleminate software
bugs before they impact guix users. Not all packages have tests, and guix
developers occasionally disables some packages' testsuites, but the
guix policy is to run the software's testsuite, if it is exists.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Install.&lt;/p&gt;&lt;p&gt;Now that we've verified everything works as expected, it is time to run
&lt;code&gt;make install&lt;/code&gt; or the equivalent.  This phase copies our build artifacts into
their final resting place in the store.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Validate the Installation.&lt;/p&gt;&lt;p&gt;Here guix checks that our outputs do not contain any component paths, which
are not specified by the package inputs. That would lead to incomplete
deployment, harm &lt;a href=&quot;https://reproducible-builds.org/&quot;&gt;reproducible builds&lt;/a&gt;,
and would be &amp;quot;bad&amp;quot;.&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;There are more steps, but they are not universally applicable.
Of course no generic model, such as this, captures all the chaos of the
world, and every package has exceptions to it.&lt;/p&gt;&lt;p&gt;Guix implements &lt;em&gt;build phases&lt;/em&gt; as a list of lambdas.
As such our package can modify this list and add/delete/replace
lambdas as they require. It is so common Guix provides a syntax
for manipulating build phases: &lt;code&gt;modify-phases&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;A build system contains a default set of phases called &lt;code&gt;%standard-phases&lt;/code&gt;.
Every build system starts with the &lt;code&gt;gnu-build-system&lt;/code&gt;, &lt;code&gt;%standard-phases&lt;/code&gt;,
and modifies it to their needs.
It is then provided to the packages using that build system.&lt;/p&gt;&lt;p&gt;The &lt;code&gt;cmake-build-system&lt;/code&gt; is nearly identical to the &lt;code&gt;gnu-build-system&lt;/code&gt;
except two phases.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;;;
;; Excerpt from `guix/build/cmake-build-system.scm`
;;
(define %standard-phases
  ;; Everything is the same as the GNU Build System, except for the `configure'
  ;; and 'check' phases.
  (modify-phases gnu:%standard-phases
    (delete 'bootstrap)
    (replace 'check check)
    (replace 'configure configure)))&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;The Zephyr Build System&lt;/h2&gt;&lt;p&gt;Zephyr uses &lt;code&gt;cmake&lt;/code&gt; to perform &lt;em&gt;physical component composition&lt;/em&gt;.
It searches the filesystem and generates scripts, which the toolchain will
use to successfully combine those components into a firmware image.&lt;/p&gt;&lt;p&gt;The fact that Zephyr provides this mechanism is one reason I chose to
target zephyr in the first place.&lt;/p&gt;&lt;p&gt;This separation of projects in an embedded context is a really great thing.
It brings many of the advantages to the Linux world such as
code re-use, smaller binaries, more efficient cache/RAM usage, etc.
It also allows us to work as independent groups and compose
contributions from many teams.&lt;/p&gt;&lt;p&gt;It also brings all of the complexity. Suddenly all of the problems
that plague traditional deployment now apply to our embedded
system. The fact that the libraries are statically linked at compile
time instead of dynamically at runtime is simply an implementation detail.&lt;/p&gt;&lt;p&gt;We now have dependencies which we must track and compose in the correct environments.
The Zephyr Project offers a tool to help manage this new complexity: &lt;a href=&quot;https://docs.zephyrproject.org/latest/develop/west/index.html&quot;&gt;The West Meta-tool&lt;/a&gt;!
To call it a &amp;quot;meta-tool&amp;quot; is an abuse of language.  It is not even meta
enough to bootstrap itself and relies on &lt;a href=&quot;https://docs.zephyrproject.org/latest/develop/getting_started/index.html&quot;&gt;other package managers&lt;/a&gt; to get
started.
In fact, it is not even suitable for managing multiple embedded projects as a composition!
The project recommends &lt;a href=&quot;https://docs.zephyrproject.org/latest/build/sysbuild/index.html&quot;&gt;Yet Another Tool&lt;/a&gt; for this very common use case.&lt;/p&gt;&lt;p&gt;In fact, West does not really bring anything new to the table, and we can
replace it in the same way we can replace every other language specific package
manager, like node (js), Cabol (haskell), Dub (D), etc. &lt;strong&gt;PUTTING SOFTWARE ON
THE SYSTEM IS THE JOB OF THE PACKAGE MANAGER!&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;West is the way it is for a reason. It is very practical to design the package
manager in this way, because it enables Windows users to access the build
environment. A more in-depth discussion on the material conditions, which lead
to this or that design decision of the West tool is beyond the scope of this
post. Let's instead explain how to provide a meta-tool and bootstrap complex
embedded systems, from the ground up, in a flexible, composable, and
&lt;em&gt;reproducible&lt;/em&gt; way.&lt;/p&gt;&lt;h1&gt;Why not use &lt;code&gt;cmake-build-system&lt;/code&gt;?&lt;/h1&gt;&lt;p&gt;A fair question! Zephyr's CMake scripts require additional information
about the build to be provided at configure time. Most tedius for
zephyr-packages is the &lt;code&gt;ZEPHYR_MODULES&lt;/code&gt; variable, which must be formatted and
passed to CMake on the command line.&lt;/p&gt;&lt;h1&gt;Host Side&lt;/h1&gt;&lt;p&gt;Our job at this level is to take packages described using the &lt;code&gt;package&lt;/code&gt; syntax and
lower it into a derivation that the guix-daemon can understand.&lt;/p&gt;&lt;p&gt;Here is the derivation for building hello world for the &lt;code&gt;frdm_k64f&lt;/code&gt; (hashes removed for brevity).
The &lt;code&gt;package&lt;/code&gt; syntax provides a human friendly veneer over this garbage.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot;&gt;Derive
    ([(&amp;quot;debug&amp;quot;,&amp;quot;/gnu/store/...-zephyr-hello-world-newlib-frdm-k64f-3.1.0-0.zephyr--debug&amp;quot;,&amp;quot;&amp;quot;,&amp;quot;&amp;quot;)
      ,(&amp;quot;out&amp;quot;,&amp;quot;/gnu/store/...-zephyr-hello-world-newlib-frdm-k64f-3.1.0-0.zephyr-&amp;quot;,&amp;quot;&amp;quot;,&amp;quot;&amp;quot;)]
     ,[(&amp;quot;/gnu/store/...-newlib-nano-3.3.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-hal-nxp-3.1.0-0.708c958.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-make-4.3.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-findutils-4.8.0.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-grep-3.6.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-sed-4.8.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-ld-wrapper-0.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-bash-minimal-5.1.8.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-hal-cmsis-5.8.0-0.093de61.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-gawk-5.1.0.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-gzip-1.10.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-python-six-1.16.0.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-zephyr-3.1.0-0.zephyr--checkout.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-linux-libre-headers-5.10.35.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-python-3.9.9.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-diffutils-3.8.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-arm-zephyr-eabi-nano-toolchain-12.1.0.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-python-pyelftools-0.28.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-guile-3.0.7.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-python-dateutil-2.8.2.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-patch-2.7.6.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-gcc-10.3.0.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-bzip2-1.0.8.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-dtc-1.6.1.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-gcc-cross-sans-libc-arm-zephyr-eabi-12.1.0.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-cmake-minimal-3.21.4.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-python-pyyaml-6.0.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-python-packaging-21.3.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-arm-zephyr-eabi-binutils-2.38.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-glibc-2.33.drv&amp;quot;,[&amp;quot;out&amp;quot;,&amp;quot;static&amp;quot;])
       ,(&amp;quot;/gnu/store/...-qemu-7.2.0.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-ninja-1.10.2.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-tar-1.34.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-xz-5.2.5.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-binutils-2.37.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-python-pykwalify-1.7.0.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-zephyr-3.1.0-0.zephyr-.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-glibc-utf8-locales-2.33.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-gdb-arm-zephyr-eabi-12.1.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-module-import-compiled.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-file-5.39.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-python-pyparsing-3.0.6.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-python-docopt-0.6.2.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-arm-zephyr-eabi-sdk-0.15.0.drv&amp;quot;,[&amp;quot;out&amp;quot;])
       ,(&amp;quot;/gnu/store/...-coreutils-8.32.drv&amp;quot;,[&amp;quot;out&amp;quot;])]
     ,[&amp;quot;/gnu/store/...-zephyr-hello-world-newlib-frdm-k64f-3.1.0-0.zephyr--builder&amp;quot;,&amp;quot;/gnu/store/...-module-import&amp;quot;]
     ,&amp;quot;x86_64-linux&amp;quot;,&amp;quot;/gnu/store/...-guile-3.0.7/bin/guile&amp;quot;,[&amp;quot;--no-auto-compile&amp;quot;
     ,&amp;quot;-L&amp;quot;,&amp;quot;/gnu/store/...-module-import&amp;quot;
     ,&amp;quot;-C&amp;quot;,&amp;quot;/gnu/store/...-module-import-compiled&amp;quot;
     ,&amp;quot;/gnu/store/...-zephyr-hello-world-newlib-frdm-k64f-3.1.0-0.zephyr--builder&amp;quot;]
     ,[(&amp;quot;debug&amp;quot;,&amp;quot;/gnu/store/...-zephyr-hello-world-newlib-frdm-k64f-3.1.0-0.zephyr--debug&amp;quot;)
       ,(&amp;quot;out&amp;quot;,&amp;quot;/gnu/store/...-zephyr-hello-world-newlib-frdm-k64f-3.1.0-0.zephyr-&amp;quot;)])&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;Lowering packages to bags&lt;/h3&gt;&lt;p&gt;We must provide the build system with a function which the following lambda form.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;(lambda* (name #:key #:allow-other-keys) ...)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This means it takes one required argument &lt;code&gt;name&lt;/code&gt; and any amount of keys.
Individual procedures can specify keys they are interested in such as
&lt;code&gt;inputs&lt;/code&gt; or &lt;code&gt;outputs&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Which keys are ultimately supported is defined by our &lt;code&gt;lower&lt;/code&gt; function
and our &lt;em&gt;build phases&lt;/em&gt;.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;;; Use module-ref instead of referencing the variables directly
;; to avoid circular dependencies.
(define %zephyr-build-system-modules
  `((mfs build zephyr-build-system)
,@%cmake-build-system-modules))

(define default-zephyr-base
  (module-ref (resolve-interface '(mfs packages zephyr))
      'zephyr))

(define default-zephyr-sdk
  (module-ref (resolve-interface '(mfs packages zephyr))
      'arm-zephyr-eabi-sdk))

(define default-ninja
  (module-ref (resolve-interface '(gnu packages ninja))
      'ninja))

(define default-cmake
  (module-ref (resolve-interface '(gnu packages cmake))
      'cmake-minimal))


(define* (lower name
        #:key source inputs native-inputs outputs system target
        (zephyr default-zephyr-base)
        (sdk default-zephyr-sdk)
        (ninja default-ninja)
        (cmake default-cmake)
        #:allow-other-keys
        #:rest arguments)
  &amp;quot;Return a bag for NAME.&amp;quot;
  (define private-keywords `(#:zephyr #:inputs #:native-inputs #:target))
  (bag
(name name)
(system system)
(target target)
(build-inputs `(,@(if source `((&amp;quot;source&amp;quot; ,source)) '())
        ,@`((&amp;quot;cmake&amp;quot; ,cmake))
        ,@`((&amp;quot;zephyr-sdk&amp;quot; ,sdk))
        ,@`((&amp;quot;zephyr&amp;quot; ,zephyr))
        ,@`((&amp;quot;ninja&amp;quot; ,ninja))
        ,@native-inputs
        ,@(standard-packages)))
;; Inputs need to be available at build time
;; since everything is statically linked.
(host-inputs inputs)
(outputs outputs)
(build zephyr-build)
(arguments (strip-keyword-arguments private-keywords arguments))))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here our &lt;code&gt;lower&lt;/code&gt; function provides default values for the packages
every zephyr package needs, the SDK, CMake, and &lt;code&gt;ZEPHYR_BASE&lt;/code&gt; and adds
them to the build-inputs.&lt;/p&gt;&lt;p&gt;Notice we also strip out some keywords, which do not get passed to the
build function, because they get included as part of the broader
abstractions the build system provides.&lt;/p&gt;&lt;p&gt;At this step it would be great to have a parser which could work out
the required sdk from a &lt;code&gt;.config,&lt;/code&gt; but this requires compiling the kconfig,
which requires at least the sdk cmake files.
There might be a way to make it happen, but until then if a board needs a different
sdk, then they can specify it in an argument keyword.&lt;/p&gt;&lt;h3&gt;Lowering Bags to Derivations&lt;/h3&gt;&lt;p&gt;Here is the definition for the actual build procedure. There is a lot
of abstract trickery going on here, so do not worry if you don't understand it,
I barely understand it!
It's mostly copy and pasted from the CMake build system.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;(define* (zephyr-build name inputs
               #:key guile source
               board
               (outputs '(&amp;quot;out&amp;quot;)) (configure-flags ''())
               (search-paths '())
               (make-flags ''())
               (out-of-source? #t)
               (tests? #f)
               (test-target &amp;quot;test&amp;quot;)
               (parallel-build? #t) (parallel-tests? #t)
               (validate-runpath? #f)
               (patch-shebangs? #t)
               (phases '%standard-phases)
               (system (%current-system))
               (substitutable? #t)
               (imported-modules %zephyr-build-system-modules)

               ;; The modules referenced here contain code
               ;; which will be staged in the build environment with us.
               ;; Our build gexp down below will only be able to access this code
               ;; and we must be careful not to reference anything else.
               (modules '((zephyr build zephyr-build-system)
                      (guix build utils))))
    &amp;quot;Build SOURCE using CMAKE, and with INPUTS. This assumes that SOURCE
    provides a 'CMakeLists.txt' file as its build system.&amp;quot;

    ;; This is the build gexp. It handles staging values from our host
    ;; system into code that our build system can run.
    (define build
    (with-imported-modules imported-modules
      #~(begin
          (use-modules #$@(sexp-&amp;gt;gexp modules))
          #$(with-build-variables inputs outputs
          #~(zephyr-build #:source #+source
                  #:system #$system
                  #:outputs %outputs
                  #:inputs %build-inputs
                  #:board #$board
                  #:search-paths '#$(sexp-&amp;gt;gexp
                             (map search-path-specification-&amp;gt;sexp
                              search-paths))
                  #:phases #$(if (pair? phases)
                         (sexp-&amp;gt;gexp phases)
                         phases)
                  #:configure-flags #$(if (pair? configure-flags)
                              (sexp-&amp;gt;gexp configure-flags)
                              configure-flags)
                  #:make-flags #$make-flags
                  #:out-of-source? #$out-of-source?
                  #:tests? #$tests?
                  #:test-target #$test-target
                  #:parallel-build? #$parallel-build?
                  #:parallel-tests? #$parallel-tests?
                  #:validate-runpath? #$validate-runpath?
                  #:patch-shebangs? #$patch-shebangs?
                  #:strip-binaries? #f)))))

    (mlet %store-monad ((guile (package-&amp;gt;derivation (or guile (default-guile))
                              system #:graft? #f)))
    (gexp-&amp;gt;derivation name build
              #:system system
              #:target #f
              #:graft? #f
              #:substitutable? substitutable?
              #:guile-for-build guile)))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Finally we define our build system which the package definitions can reference.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;(define zephyr-build-system
  (build-system
    (name 'zephyr)
    (description &amp;quot;The standard Zephyr build system&amp;quot;)
    (lower lower)))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Easy right?&lt;/p&gt;&lt;h1&gt;Build Side&lt;/h1&gt;&lt;p&gt;The build side is not as complex as you might initially expect.
Our build system is almost exactly the same as the CMake build system
except our configure phase passes different values to CMake.
Our job is much easier.&lt;/p&gt;&lt;h3&gt;Locating Modules&lt;/h3&gt;&lt;p&gt;Zephyr CMake requires the zephyr &lt;em&gt;modules&lt;/em&gt; which are needed for the
build to be supplied on the command line.
Unfortunately for us, the
&lt;a href=&quot;https://docs.zephyrproject.org/3.1.0/develop/application/index.html#important-build-vars&quot;&gt;documentation&lt;/a&gt;
is wrong, and the &lt;code&gt;ZEPHYR_MODULES&lt;/code&gt; environment variable is not honored.
Thus we must implement some other solution for locating modules, until
this that is fixed.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Input Scanning&lt;/strong&gt; - Lucky for us we are keeping detailed information
about our dependencies. It is a simple matter to write a file tree
walker which collects all the zephyr modules in our inputs.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;(define* (find-zephyr-modules directories)
  &amp;quot;Return the list of directories containing zephyr/module.yml found
   under DIRECTORY, recursively. Return the empty list if DIRECTORY is
   not accessible.&amp;quot;
  (define (module-directory file)
    (dirname (dirname file)))

  (define (enter? name stat result)
    ;; Skip version control directories.
    ;; Shouldn't be in the store but you never know.
    (not (member (basename name) '(&amp;quot;.git&amp;quot; &amp;quot;.svn&amp;quot; &amp;quot;CVS&amp;quot;))))

  (define (leaf name stat result)
    ;; Add module root directory to results
    (if (and (string= &amp;quot;module.yml&amp;quot; (basename name))
         (string= &amp;quot;zephyr&amp;quot; (basename (dirname name))))
      (cons (module-directory name) result)
      result))

  (define (down name stat result) result)
  (define (up name stat result) result)
  (define (skip name stat result) result)

  (define (find-modules directory)
    (file-system-fold enter? leaf down up skip error
              '() (canonicalize-path directory)))

  (append-map find-modules directories))

(define (zephyr-modules-cmake-argument modules)
   &amp;quot;Return a proper CMake list from MODULES, a list of filepaths&amp;quot;
   (format #f &amp;quot;-DZEPHYR_MODULES='~{~a~^;~}'&amp;quot; modules))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here are two functions. The first one &lt;code&gt;find-zephyr-modules&lt;/code&gt; walks a
list of directories (package inputs) and returns a list of every module it finds.
The second one is just for syntactic convenience when writing the CMake invokation.
This is also slightly more robust than West's module discovery, because it allows for
a single repository to provide multiple modules which are not &lt;em&gt;technically&lt;/em&gt; required
to be at the top level.&lt;/p&gt;&lt;p&gt;From here we just need to provide alternate implementations of &lt;code&gt;configure&lt;/code&gt; and &lt;code&gt;install&lt;/code&gt;.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;(define* (configure #:key outputs (configure-flags '())
            inputs (out-of-source? #t)
            build-type
            #:allow-other-keys)
      &amp;quot;Configure the given package.&amp;quot;
      (let* ((out        (assoc-ref outputs &amp;quot;out&amp;quot;))
         (abs-srcdir (getcwd))
         (srcdir     (if out-of-source?
                 (string-append &amp;quot;../&amp;quot; (basename abs-srcdir))
                 &amp;quot;.&amp;quot;)))
    (format #t &amp;quot;source directory: ~s (relative from build: ~s)~%&amp;quot;
        abs-srcdir srcdir)
    (when out-of-source?
      (mkdir &amp;quot;../build&amp;quot;)
      (chdir &amp;quot;../build&amp;quot;))
    (format #t &amp;quot;build directory: ~s~%&amp;quot; (getcwd))

    ;; this is required because zephyr tries to optimize
    ;; future calls to the build scripts by keep a cache.
    (setenv &amp;quot;XDG_CACHE_HOME&amp;quot; (getcwd))

    (let ((args `(,srcdir
              ,@(if build-type
                (list (string-append &amp;quot;-DCMAKE_BUILD_TYPE=&amp;quot;
                         build-type))
                '())
              ;; enable verbose output from builds
              &amp;quot;-DCMAKE_VERBOSE_MAKEFILE=ON&amp;quot;
              ,(zephyr-modules-cmake-argument
            (find-zephyr-modules (map cdr inputs)))
              ,@configure-flags)))
      (format #t &amp;quot;running 'cmake' with arguments ~s~%&amp;quot; args)
      (apply invoke &amp;quot;cmake&amp;quot; args))))

    (define* (install #:key outputs #:allow-other-keys)
      (let* ((out (string-append (assoc-ref outputs &amp;quot;out&amp;quot;) &amp;quot;/lib/firmware&amp;quot;))
         (dbg (string-append (assoc-ref outputs &amp;quot;debug&amp;quot;) &amp;quot;/share/zephyr&amp;quot;)))
    (mkdir-p out)
    (mkdir-p dbg)
    (copy-file &amp;quot;zephyr/.config&amp;quot; (string-append dbg &amp;quot;/config&amp;quot;))
    (copy-file &amp;quot;zephyr/zephyr.map&amp;quot; (string-append dbg &amp;quot;/zephyr.map&amp;quot;))
    (copy-file &amp;quot;zephyr/zephyr.elf&amp;quot; (string-append out &amp;quot;/zephyr.elf&amp;quot;))
    (copy-file &amp;quot;zephyr/zephyr.bin&amp;quot; (string-append out &amp;quot;/zephyr.bin&amp;quot;))))

;; Define new standard-phases
(define %standard-phases
      (modify-phases cmake:%standard-phases
    (replace 'configure configure)
    (replace 'install install)))

;; Call cmake build with our new phases
(define* (zephyr-build #:key inputs (phases %standard-phases)
               #:allow-other-keys #:rest args)
  (apply cmake:cmake-build #:inputs inputs #:phases phases args))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;One thing to note is the &amp;quot;debug&amp;quot; output. This exists so we don't
retain references to our build environment and make the file system
closure huge. If you put all of the build outputs in the same store
path, then the deployment closure will grow from 2MB to 833MB.&lt;/p&gt;&lt;h1&gt;Defining Zephyr Packages&lt;/h1&gt;&lt;p&gt;Now that we have a proper build system, it's time to define some packages!&lt;/p&gt;&lt;h3&gt;Zephyr Base&lt;/h3&gt;&lt;p&gt;Zephyr base contains the Zephyr source code. It is equivalent (in my mind
anyway) to the linux kernel, in that packages' definitions' specifications,
which target the linux kernel, can be minimal.&lt;/p&gt;&lt;p&gt;The selection of operating system actually comes from the toolchain. For
example, we build linux packages with the &lt;a href=&quot;https://www.gnu.org/software/autoconf/manual/autoconf-2.65/html_node/Specifying-Target-Triplets.html&quot;&gt;gnu
triplet&lt;/a&gt;.
When we select the &lt;code&gt;arm-linux-gnueabihf,&lt;/code&gt; we are specifying our operating system.&lt;/p&gt;&lt;p&gt;It is the same for Zephyr. When we build for zephyr we use the &lt;code&gt;arm-zephyr-eabi&lt;/code&gt;
toolchain. However, unlike linux applications, zephyr applications are
embedded firmware images and are generally statically linked.
Thus this package just consists of its source code and is not compiled directly.
We cannot compile it now because applications/modules provide the required Kconfig
options.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;(define-public zephyr
  (let ((version &amp;quot;3.1.0&amp;quot;)
    (commit &amp;quot;zephyr-v3.1.0&amp;quot;))
    (package
      (name &amp;quot;zephyr&amp;quot;)
      (version version)
      (home-page &amp;quot;https://zephyrproject.org&amp;quot;)
      (source (origin (method git-fetch)
              (uri (git-reference
                (url &amp;quot;https://github.com/zephyrproject-rtos/zephyr&amp;quot;)
                (commit commit)))
              (file-name (git-file-name name version))
              (sha256
               (base32 &amp;quot;1yl5y9757xc3l037k3g1dynispv6j5zqfnzrhsqh9cx4qzd485lx&amp;quot;))
              (patches
               ;; this patch makes this package work in a symlinked profile
               (search-patches &amp;quot;zephyr-3.1-linker-gen-abs-path.patch&amp;quot;))))
      (build-system copy-build-system)
      (arguments
       `(#:install-plan
     '((&amp;quot;.&amp;quot; &amp;quot;zephyr-workspace/zephyr&amp;quot;))
     #:phases
     (modify-phases %standard-phases
       (add-after 'unpack 'patch-cmake-scripts
         (lambda* _
           (format #t &amp;quot;~a~&amp;amp;&amp;quot; (getcwd))
           ;; Some cmake scripts assume the presence of a
           ;; git repository in the source directory.
           ;; We will just hard-code that information now
           (substitute* &amp;quot;CMakeLists.txt&amp;quot;
         ((&amp;quot;if\\(DEFINED BUILD_VERSION\\)&amp;quot; all)
          (format #f &amp;quot;set(BUILD_VERSION \&amp;quot;~a-~a\&amp;quot;)~&amp;amp;~a&amp;quot;
              ,version ,commit all))))))))
      (propagated-inputs
       (list python-3
         python-pyelftools
         python-pykwalify
         python-pyyaml
         python-packaging))
      (native-search-paths
       (list (search-path-specification
          (variable &amp;quot;ZEPHYR_BASE&amp;quot;)
          (files '(&amp;quot;zephyr-workspace/zephyr&amp;quot;)))))
      (synopsis &amp;quot;Source code for zephyr rtos&amp;quot;)
      (description &amp;quot;Zephyr rtos source code.&amp;quot;)
      (license license:apsl2))))

(define-public zephyr-3.2.0-rc3
      (package (inherit zephyr)
    (version &amp;quot;3.2.0-rc3&amp;quot;)
    (source (origin (method git-fetch)
            (uri (git-reference
                  (url &amp;quot;https://github.com/zephyrproject-rtos/zephyr&amp;quot;)
                  (commit &amp;quot;v3.2.0-rc3&amp;quot;)))
            (file-name (git-file-name &amp;quot;zephyr&amp;quot; version))
            (sha256
             (base32 &amp;quot;06ksd9zj4j19jq0zg3lms13jx0gxzjc41433zgb91cnd2cqmn5cb&amp;quot;))
            (patches
             (search-patches &amp;quot;zephyr-3.1-linker-gen-abs-path.patch&amp;quot;))))))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here we use the &lt;code&gt;copy-build-system&lt;/code&gt; which takes a list of source destination
pairs. In our case, we just copy everything to the output directory, but not
before patching some files to accomodate our special environment.&lt;/p&gt;&lt;p&gt;While developing this I wanted to test some toolchain/board features
on the latest release of Zephyr. I included an example of that package
definition to show how we can easily accomodate side by side package
variants and experiment without breaking anything.&lt;/p&gt;&lt;h3&gt;Modules&lt;/h3&gt;&lt;p&gt;It's finally time to define some firmware!
Zephyr packages some examples in &lt;code&gt;$ZEPHYR_BASE/samples&lt;/code&gt; including a
basic hello world. The k64 development board is already supported
by Zephyr so building the example is trivial.&lt;/p&gt;&lt;p&gt;In order to actually target the k64 we need to two modules, the NXP
hardware abstraction layer, and CMSIS.
Looking at &lt;code&gt;$ZEPHYR_BASE/west.yml&lt;/code&gt; we can see the repositories
and commits which contain these modules. This is how West does
dependency management.&lt;/p&gt;&lt;p&gt;Defining these packages is not so bad (see footnote 1).&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;(define-public hal-cmsis
      (package
    (name &amp;quot;hal-cmsis&amp;quot;)
    (version &amp;quot;5.8.0&amp;quot;)
    (home-page &amp;quot;https://developer.arm.com/tools-and-software/embedded/cmsis&amp;quot;)
    (source (origin
          (method git-fetch)
          (uri (git-reference
            (url &amp;quot;https://github.com/zephyrproject-rtos/cmsis&amp;quot;)
            (commit &amp;quot;093de61c2a7d12dc9253daf8692f61f793a9254a&amp;quot;)))
          (file-name (git-file-name name version))
          (sha256
           (base32 &amp;quot;0f7cipnwllna7iknsnz273jkvrly16yr6wm4y2018i6njpqh67wi&amp;quot;))))
    (build-system zephyr-module-build-system)
    (arguments `(#:workspace-path  &amp;quot;/modules/hal/cmsis&amp;quot;))
    (synopsis &amp;quot;Zephyr module providing the Common Microcontroller
    Software Interface Standard&amp;quot;)
    (description &amp;quot;Zephyr module providing the Common Microcontroller
    Software Interface Standard&amp;quot;)
    (license license:apsl2)))

(define-public hal-nxp
      (package
    (name &amp;quot;hal-nxp&amp;quot;)
    (version &amp;quot;3.1.0&amp;quot;)
    (home-page &amp;quot;https://nxp.com&amp;quot;)
    (source (origin
          (method git-fetch)
          (uri (git-reference
            (url &amp;quot;https://github.com/zephyrproject-rtos/hal_nxp&amp;quot;)
            (commit &amp;quot;708c95825b0d5279620935a1356299fff5dfbc6e&amp;quot;)))
          (file-name (git-file-name name version))
          (sha256
           (base32 &amp;quot;1c0i26bpk6cyhr1q4af183jclfmxshv4d15i7k3cz7brzb12m8q1&amp;quot;))))
    (build-system zephyr-module-build-system)
    (arguments `(#:workspace-path  &amp;quot;/modules/hal/nxp&amp;quot;))
    (native-search-paths
     (list (search-path-specification
        (variable &amp;quot;ZEPHYR_MODULES&amp;quot;)
        (files `(,(string-append %zephyr-workspace-name module-path)))
        (separator &amp;quot;;&amp;quot;))))
    (synopsis &amp;quot;Zephyr module for NXP Hardware Abstraction Layer&amp;quot;)
    (description &amp;quot;Provides sources for NXP HAL zephyr module&amp;quot;)
    (license license:bsd-3)))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;With these two modules defined we can write &lt;code&gt;zephyr-hello-world-frdm-k64f&lt;/code&gt;.&lt;/p&gt;&lt;h3&gt;Hello world&lt;/h3&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;(define-public zephyr-hello-world-frdm-k64f
      (package
    (name &amp;quot;zephyr-hello-world-frdm-k64f&amp;quot;)
    (version (package-version zephyr))
    (home-page &amp;quot;https://zephyrproject.org&amp;quot;)
    (source (file-append (package-source zephyr)
                 &amp;quot;/samples/hello_world&amp;quot;))
    (build-system zephyr-build-system)
    (arguments
     '(#:configure-flags '(&amp;quot;-DBOARD=frdm_k64f&amp;quot;)))
    (outputs '(&amp;quot;out&amp;quot; &amp;quot;debug&amp;quot;))
    (inputs
     (list hal-cmsis
           hal-nxp))
    (synopsis &amp;quot;Hello world example from Zephyr Project&amp;quot;)
    (description &amp;quot;Sample package for zephyr project&amp;quot;)
    (license license:apsl2)))&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;Building&lt;/h3&gt;&lt;p&gt;Our above definition can be built using the following:
When testing package definitions, I use the &lt;code&gt;-L&lt;/code&gt; flag to point to the local
repository to load the new packages. I will be omitting that flag as
if I had ~guix pull~ed successfully from &lt;a href=&quot;https://github.com/paperclip4465/guix-zephyr&quot;&gt;guix-zephyr&lt;/a&gt;.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot;&gt;guix build zephyr-hello-world-frdm-k64f

/gnu/store/...-zephyr-hello-world-frdm-k64f-3.1.0-debug
/gnu/store/...-zephyr-hello-world-frdm-k64f-3.1.0&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This actually doesn't fully test our toolchain. The hello world
example, by default, will use a zephyr provided
minimal implementation of the C library and will not link against
newlib.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;(define-public zephyr-hello-world-newlib-frdm-k64f
      (package
    (inherit zephyr-hello-world-frdm-k64f)
    (name &amp;quot;zephyr-hello-world-newlib-frdm-k64f&amp;quot;)
    (arguments
     (substitute-keyword-arguments (package-arguments zephyr-hello-world-frdm-k64f)
       ((#:configure-flags flags)
        `(append
          '(&amp;quot;-DCONFIG_MINIMAL_LIBC=n&amp;quot;
        &amp;quot;-DCONFIG_NEWLIB_LIBC=y&amp;quot;)
          ,flags))))))

    guix build zephyr-hello-world-newlib-frdm-k64f

/gnu/store/...-zephyr-hello-world-newlib-frdm-k64f-3.1.0-debug
/gnu/store/...-zephyr-hello-world-newlib-frdm-k64f-3.1.0&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Woohoo!&lt;/p&gt;&lt;h1&gt;Further Musings&lt;/h1&gt;&lt;p&gt;One thing I learned while going through the pains of getting this
working is that even though the components are &amp;quot;modular&amp;quot; there is
still a lot rigid interdependencies, especially on the zephyr base.
Just having two versions of Zephyr in the code base made component
composition fragile.
Modules rely on specific features from the kernel.
This is hidden from developers normally by west automagically walking
the `west.yml` of all of the declared dependencies recursively to
discover the graph.&lt;/p&gt;&lt;p&gt;While there are many benefits to a modularized build system, a monolithic build
system, like Zephyr, does have many benefits too.&lt;/p&gt;&lt;p&gt;Part of the problem comes from the domain itself. If you really want to be able
to target the most resource constrained systems and deal with the &amp;quot;industrial
mess&amp;quot; that comes from every board being unique, you have to be as generic and
flexible as possible, which is hard in a guix-like modular build system.&lt;/p&gt;&lt;p&gt;Superficially the problem is solved in the same way Linux solved it, using
device trees and having a very stable userland interface. However, unlike Linux
where the device tree is compiled to a binary blob and interpreted by drivers at
runtime, Zephyr device trees are compiled to a &lt;code&gt;.h&lt;/code&gt; file and are mostly
interpreted by the C pre-processor using an elaborate set of macros.&lt;/p&gt;&lt;p&gt;It goes beyond simply abstracting the hardware using clever hacks.
Zephyr applications (and any zephyr module) can also introduce new
&amp;quot;kernel&amp;quot; code, configuration options, and even linker script fragments
at build time.
Essentially the Zephyr CMake build system acts like a reverse ld.
Instead of linking libraries after compilation, it discovers these
things before gcc is ever invoked and provides additional code
generation steps.&lt;/p&gt;&lt;p&gt;Zephyr does not have a stable &amp;quot;userland&amp;quot; interface for the same
reason Linux does not have a stable &amp;quot;kernel module&amp;quot; interface.
Because Zephyr applications are so tightly coupled to the hardware
they run on it is not uncommon to bypass Zephyr utilities and directly
touch hardware and memory.&lt;/p&gt;&lt;p&gt;In this way they are more related to kernel modules
than userspace applications such as GNU Hello.&lt;/p&gt;&lt;p&gt;Perhaps there is a lispy way to track several zephyr releases without reducing
the ability to freely modify components in the usual ways...I invite you dear
reader to developer code to explore that possibility.&lt;/p&gt;&lt;p&gt;It is use-able for Guix anyway.&lt;/p&gt;&lt;h1&gt;Why a Special Build System? Why not &lt;code&gt;--target=frdm_k64f&lt;/code&gt;?&lt;/h1&gt;&lt;p&gt;That is a fair question!
At first glance you might imagine the following incantation:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot;&gt;guix build --target=arm-zephyr-eabi hello&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The problem with calling the toolchain directly is that the architecture
is specified by the board selection. It is not generally useful to
compile a board to a different architecture.&lt;/p&gt;&lt;p&gt;Perhaps maybe something like this then.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot;&gt;guix build --target=frdm_k64f hello&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The above command tells GNU hello to link against arm-zephyr-newlib and run on
a specific board. The problem is that while this &lt;em&gt;may&lt;/em&gt; work for GNU Hello, it
will not work for anything, which requires inputs that are discovered by normal
methods. Only packages which target zephyr explicitly could benefit from such an
interface, and at that point, you may as well record which board is being targeted
in the package definition.&lt;/p&gt;&lt;p&gt;In general not all zephyr applications can run on every board zephyr
can run on, so the usefulness of the above command is dubious.&lt;/p&gt;&lt;p&gt;I think if you have firmware which targets multiple boards it is
better to define a package for every board. It is likely every board
will require special configuration flags anyway.&lt;/p&gt;&lt;h1&gt;Conclusion&lt;/h1&gt;&lt;p&gt;Zephyr has a very complex build process which can be difficult to
understand and frustrating to set up.&lt;/p&gt;&lt;p&gt;Using Guix to define firmware packages makes these problems disappear.
It is trivial to create a channel which contains all of the quirks of
your platform and share it with a team or student.&lt;/p&gt;&lt;p&gt;Packages defined this way serve as a reproducible starting point for
future hardware revisions and variations.&lt;/p&gt;&lt;h1&gt;Footnotes&lt;/h1&gt;&lt;ol&gt;&lt;li&gt;I also made a &lt;code&gt;zephyr-module-build-system&lt;/code&gt; as well which is just the
&lt;code&gt;copy-build-system&lt;/code&gt; that mimics the default zephyr workspace layout as provided
by west. This way we do not need to provide the same install-plan for every
module we package. However as I use the &lt;code&gt;copy-build-system&lt;/code&gt; more often, it
doesn't really provide much over just using the copy-build system.&lt;/li&gt;&lt;/ol&gt;</content></entry></feed>