|
flake.nix
|
The first thing to do is to create a `flake.nix` file.
|
{
description = "My own hello world";
|
a flake must contain an `outputs` function that takes an attribute set of at least `self` and an optional number of packages to use (here just `nixpkgs`).
|
outputs = { self, nixpkgs }:
|
The first thing we do here, is to create a function that we will reuse to build our package for different systems (mac and linux). This function takes one argument: `system`.
|
let
build_for = system:
|
We import `nixpkgs` with the `system` that we passed as argument (same as writing `system = system;` in the second argument of `import`.
|
let
pkgs = import nixpkgs { inherit system; };
in
|
`stdenv.mkDerivation` allows us to create a **derivation**, which is nix's term for a package.
|
pkgs.stdenv.mkDerivation {
name = "hello";
src = self;
buildInputs = [ pkgs.gcc pkgs.which ];
buildPhase = "gcc -o hello ./hello.c";
installPhase = "mkdir -p $out/bin; install -t $out/bin hello";
};
in
|
the `output` function must return an attribute set containing default packages for one or more platforms.
|
{
packages.x86_64-darwin.default = build_for "x86_64-darwin";
packages.x86_64-linux.default = build_for "x86_64-linux";
};
}
|