diff --git a/source/recipes/build/nix-non-flake/flake.nix b/source/recipes/build/nix-non-flake/flake.nix new file mode 100644 index 0000000..53107d1 --- /dev/null +++ b/source/recipes/build/nix-non-flake/flake.nix @@ -0,0 +1,53 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs"; + flake-utils.url = "github:numtide/flake-utils"; + hello-nix = { + url = "git+https://codeberg.org/mhwombat/hello-nix"; + flake = false; + }; + }; + + outputs = { self, nixpkgs, flake-utils, hello-nix }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + }; + helloNix = import hello-nix { inherit pkgs; }; + 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-nix + HELLO=$(type -p hello-nix) + sed "s_hello-nix_"$HELLO"_" --in-place $out/bin/hello-again + ''; + + + buildInputs = [ helloNix ]; + }; + default = hello; + }; + + apps = rec { + hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; }; + default = hello; + }; + } + ); +} diff --git a/source/recipes/build/nix-non-flake/hello-again b/source/recipes/build/nix-non-flake/hello-again new file mode 100755 index 0000000..5315da4 --- /dev/null +++ b/source/recipes/build/nix-non-flake/hello-again @@ -0,0 +1,4 @@ +#!/usr/bin/env sh + +echo "I'm a flake, but I'm running a command defined in a non-flake package." +hello-nix diff --git a/source/recipes/build/nix-non-flake/main.adoc0 b/source/recipes/build/nix-non-flake/main.adoc0 new file mode 100644 index 0000000..6367263 --- /dev/null +++ b/source/recipes/build/nix-non-flake/main.adoc0 @@ -0,0 +1,54 @@ += Access a non-flake package (not in nixpkgs) + +//// +$ mkdir tempwork +$ cd tempwork +$ cp ../hello-again ../flake.nix . +$ git add hello-again flake.nix +$ nix develop +$ git add flake.lock +$ git commit -a -m temp +//// + +In this example, we will use a nix package (not a flake) defined in a remote git repo. +However, you can use any of the flake reference styles defined in <<#flakeref>>. + +We already covered how to add a non-flake input to a flake and build it in <<#devshell-nix-non-flake>>; +here we will focus on making it available at runtime. +We will write a flake to package a very simple shell script. +All it does is invoke `hello-nix`, which is the input we added <<#devshell-nix-non-flake,earlier>>. + +[source,shell,linenums] +.hello-again +.... +$# cat hello-again +.... + +[source,nix,linenums,highlight='31..33,36'] +.flake.nix +.... +$# cat flake.nix +.... + +Lines 5-8 and 17 were explained in <<#devshell-nix-non-flake>>. +As expected, we need to add `helloNix` as a build input, which we do in line 36. +That does make it 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 as we install it, +by specifying the full path to `hello-nix`. + +Here's a demonstration using the shell. + +.... +$ hello-nix # this will fail +$# ../../../../../start-shell nix develop <