mirror of
https://codeberg.org/mhwombat/nix-book.git
synced 2026-01-06 22:07:33 +08:00
initial commit
This commit is contained in:
parent
c054986b34
commit
a23029eb15
7 changed files with 256 additions and 0 deletions
53
source/recipes/build/nix-non-flake/flake.nix
Normal file
53
source/recipes/build/nix-non-flake/flake.nix
Normal file
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
||||
4
source/recipes/build/nix-non-flake/hello-again
Executable file
4
source/recipes/build/nix-non-flake/hello-again
Executable file
|
|
@ -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
|
||||
54
source/recipes/build/nix-non-flake/main.adoc0
Normal file
54
source/recipes/build/nix-non-flake/main.adoc0
Normal file
|
|
@ -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 <<EOL
|
||||
$ hello-nix
|
||||
$# EOL
|
||||
....
|
||||
|
||||
////
|
||||
Good adoc0 scripts clean up after themselves.
|
||||
$ cd .. ; rm -rf tempwork # clean up
|
||||
////
|
||||
45
source/recipes/build/nixpkg/flake.nix
Normal file
45
source/recipes/build/nixpkg/flake.nix
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
{
|
||||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils }:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
in
|
||||
{
|
||||
packages = rec {
|
||||
hello = pkgs.stdenv.mkDerivation rec {
|
||||
name = "hello-with-cow";
|
||||
|
||||
src = ./.;
|
||||
|
||||
unpackPhase = "true";
|
||||
|
||||
buildPhase = ":";
|
||||
|
||||
installPhase =
|
||||
''
|
||||
mkdir -p $out/bin
|
||||
cp $src/hello-with-cow $out/bin
|
||||
chmod +x $out/bin/hello-with-cow
|
||||
|
||||
# modify the hello-again script so it can find hello-nix
|
||||
COWSAY=$(type -p cowsay)
|
||||
sed "s_cowsay_"$COWSAY"_" --in-place $out/bin/hello-with-cow
|
||||
'';
|
||||
|
||||
buildInputs = [ pkgs.cowsay ];
|
||||
};
|
||||
default = hello;
|
||||
};
|
||||
|
||||
apps = rec {
|
||||
hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
|
||||
default = hello;
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
||||
3
source/recipes/build/nixpkg/hello-with-cow
Normal file
3
source/recipes/build/nixpkg/hello-with-cow
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
cowsay "Moo!"
|
||||
34
source/recipes/build/nixpkg/main.adoc0
Normal file
34
source/recipes/build/nixpkg/main.adoc0
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
= Access a top level package from the Nixpkgs/NixOS repo
|
||||
|
||||
////
|
||||
$ mkdir tempwork
|
||||
$ cd tempwork
|
||||
$ cp ../hello-with-cow ../flake.nix .
|
||||
$ git add hello-with-cow flake.nix
|
||||
$ nix develop
|
||||
$ git add flake.lock
|
||||
$ git commit -a -m temp
|
||||
////
|
||||
|
||||
[source,shell,linenums]
|
||||
.hello-with-cow
|
||||
....
|
||||
$# cat hello-with-cow
|
||||
....
|
||||
|
||||
[source,nix,linenums,highlight=15]
|
||||
.flake.nix
|
||||
....
|
||||
$# cat flake.nix
|
||||
....
|
||||
|
||||
Here's a demonstration using the flake.
|
||||
|
||||
....
|
||||
$ nix run
|
||||
....
|
||||
|
||||
////
|
||||
Good adoc0 scripts clean up after themselves.
|
||||
$ cd .. ; rm -rf tempwork # clean up
|
||||
////
|
||||
63
source/recipes/build/non-flake/flake.nix
Normal file
63
source/recipes/build/non-flake/flake.nix
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
{
|
||||
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; };
|
||||
# helloNix = pkgs.stdenv.mkDerivation {
|
||||
# name = "hello-nix";
|
||||
# src = hello-nix;
|
||||
# installPhase =
|
||||
# ''
|
||||
# mkdir -p $out/bin
|
||||
# cp $src/hello-nix $out/bin/hello-nix
|
||||
# chmod +x $out/bin/hello-nix
|
||||
# '';
|
||||
# };
|
||||
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;
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue