From 1ad9b077c440196d4b5063d7ed00372c6df6c020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Mon, 15 Sep 2025 17:40:48 +0100 Subject: [PATCH] initial commit --- source/recipes/build/flake/flake.nix | 46 ++++++++++++++++++ source/recipes/build/flake/hello-again | 4 ++ source/recipes/build/flake/main.adoc0 | 67 ++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 source/recipes/build/flake/flake.nix create mode 100755 source/recipes/build/flake/hello-again create mode 100644 source/recipes/build/flake/main.adoc0 diff --git a/source/recipes/build/flake/flake.nix b/source/recipes/build/flake/flake.nix new file mode 100644 index 0000000..ad09046 --- /dev/null +++ b/source/recipes/build/flake/flake.nix @@ -0,0 +1,46 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs"; + flake-utils.url = "github:numtide/flake-utils"; + hello-flake.url = "git+https://codeberg.org/mhwombat/hello-flake"; + }; + + outputs = { self, nixpkgs, flake-utils, hello-flake }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { inherit system; }; + in + { + packages = rec { + hello = pkgs.stdenv.mkDerivation rec { + name = "hello-again"; + + src = ./.; + + unpackPhase = "true"; + + buildPhase = ":"; + + installPhase = + '' + mkdir -p $out/bin + cp $src/hello-again $out/bin + chmod +x $out/bin/hello-again + + # modify the hello-again script so it can find hello-flake + HELLO=$(type -p hello-flake) + sed "s_hello-flake_"$HELLO"_" --in-place $out/bin/hello-again + ''; + + buildInputs = [ hello-flake.packages.${system}.hello ]; + }; + default = hello; + }; + + apps = rec { + hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; }; + default = hello; + }; + } + ); +} diff --git a/source/recipes/build/flake/hello-again b/source/recipes/build/flake/hello-again new file mode 100755 index 0000000..f75e067 --- /dev/null +++ b/source/recipes/build/flake/hello-again @@ -0,0 +1,4 @@ +#!/usr/bin/env sh + +echo "I'm a flake, and I'm running a command defined in a another flake." +hello-flake diff --git a/source/recipes/build/flake/main.adoc0 b/source/recipes/build/flake/main.adoc0 new file mode 100644 index 0000000..b03486f --- /dev/null +++ b/source/recipes/build/flake/main.adoc0 @@ -0,0 +1,67 @@ += Access a flake + +//// +$ mkdir tempwork +$ cd tempwork +$ cp ../hello-again ../flake.nix . +$ git add hello-again flake.nix +$ nix build +$ git add flake.lock +$ git commit -a -m temp +//// + +In this example, we will use a flake defined in a remote git repo. +However, you can use any of the flake reference styles defined in <<#flakeref>>. + +[source,bash,linenums] +.hello-again +.... +$# cat hello-again +.... + +[source,nix,linenums,highlight='5,8,30..32,35'] +.flake.nix +.... +$# cat flake.nix +.... + +Line 5 adds `hello-flake` as an input to this flake, +Line 8 allows the output function to reference `hello-flake`. +As expected, we need to add `hello-flake` as a build input, which we do in line 35. +Let's take a closer look at the `buildInputs` expression from line 35. + +.... +$# grep buildInputs flake.nix | sed 's/ //g; s/.*\[//; s/\].*//' +.... + +Why is the first part `hello-flake` and the last part `hello`? +The first part refers to the name we assigned in the input section of our flake, +and the last part is the name of the package or app we want. +(See <<_flake_outputs>> for how to identify flake outputs.) + +Line 35 does make `hello-flake` available at build and runtime, +but it doesn't put it on the path, +so our `hello-again` script won't be able to find it. +There are various ways to deal with this problem. +In this case we simply edited the script (lines 30-32) as we install it, +by specifying the full path to `hello-nix`. + +[NOTE] +==== +When you're packaging a program written in a more powerful language such as +Haskell, Python, Java, C, C#, or Rust, +the language build system will usually do all that is required +to make the dependencies visible to the program at runtime. +In that case, adding the dependency to `buildInputs` is sufficient. +==== + +Here's a demonstration using the flake. + +.... +$ nix run +.... + +//// +Good adoc0 scripts clean up after themselves. +$ cd .. ; rm -rf tempwork # clean up +////