use a tiny helper module to unify output

This commit is contained in:
Cole Mickens 2020-10-25 03:59:16 -07:00
parent 72f39dbc31
commit a8a6178139
No known key found for this signature in database
GPG key ID: B475C2955744A019
6 changed files with 86 additions and 35 deletions

View file

@ -4,6 +4,9 @@ This shows how to build the same config, with and without flakes.
It also shows that `flake.nix` is basically just some syntax.
You can run `./check.sh` to show that this builds the same system config
with/without flakes.
## flake fundamentals
Nix is in flakes mode when:
@ -34,40 +37,33 @@ These three examples, for me, are all the same source, but accessed in different
## overview
Note that these produce the same output:
The `./check.sh` script proves they give the same exact system toplevel outputs:
1. with flakes:
```console
cole@slynux ~/code/nixos-flake-example master* 7s
./check.sh
```shell
nix build '.#nixosConfigurations.mysystem.config.system.build.toplevel'
readlink -f result
/nix/store/0imi716z1qd04pfh4zdw6mb0gnxmakjs-nixos-system-nixos-21.03.20201020.007126e
:: Updating the 'nixpkgs' input in flake.nix
+ nix flake update --update-input nixpkgs
+ set +x
:: Using 'nixos-rebuild' to build the 'mysystem' toplevel
+ nixos-rebuild build --flake .#mysystem
warning: Git tree '/home/cole/code/nixos-flake-example' is dirty
building the system configuration...
warning: Git tree '/home/cole/code/nixos-flake-example' is dirty
+ set +x
:: Using rev=007126eef72271480cb7670e19e501a1ad2c1ff2 for <nixpkgs> (extracted from flake.nix)
:: Setting NIX_PATH to the same values flakes is using
+ NIX_PATH=nixpkgs=https://github.com/nixos/nixpkgs/archive/007126eef72271480cb7670e19e501a1ad2c1ff2.tar.gz:nixos-config=/home/cole/code/nixos-flake-example/configuration.nix
+ nix-build '<nixpkgs/nixos>' -A config.system.build.toplevel
/nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git
+ set +x
flake: /nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git
clssc: /nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git
nixos-rebuild build --flake '.#mysystem'
readlink -f result
/nix/store/0imi716z1qd04pfh4zdw6mb0gnxmakjs-nixos-system-nixos-21.03.20201020.007126e
```
Note, nixos-rebuild is basically just some magic to build the right derivation
and then set it as a system profile, and activate it.
2. without flakes:
```shell
export NIX_PATH=nixos-config=$(pwd)/configuration.nix:nixpkgs=https://github.com/nixos/nixpkgs/archive/nixos-unstable.tar.gz
/nix/store/zidq625i13hvbbs8alkklj8k6a191xix-nixos-system-nixos-21.03pre-git
```
**Note**, ~~same path~~ same inner system, just much slower due to no eval cache.
(they should be identical, but the flake version suffix is slightly different)
They build the same thing, the flake.nix just moves the redirection from the NixOS channel system
into the flake instead.
Note, if you come back and run this later, you may need to tell nix to update the `nixpkgs` that it
has pinned in `flake.lock` by running `nix flake update --update-input nixpkgs`. The non-flake example
is going to re-download the nixos-unstable build when the cache expires. This could cause any hash differences
if they're on different revs. (again, another reason to have control of it via flakes, and can lock it directly in the source.)

46
check.sh Executable file
View file

@ -0,0 +1,46 @@
#!/usr/bin/env bash
set -euo pipefail
rm -f result
unset NIX_PATH
echo
echo ":: Updating the 'nixpkgs' input in flake.nix"; set -x
nix flake update --update-input nixpkgs &>/dev/null
set +x
echo
echo ":: Using 'nixos-rebuild' to build the 'mysystem' toplevel"; set -x
nixos-rebuild build --flake '.#mysystem'
set +x
flake_path="$(readlink -f ./result)"
# extract rev from flake.lock so we can figure out the nixpkgs rev used
rev="$(cat flake.lock| jq -r '.nodes.nixpkgs.locked.rev')"
echo
echo ":: Using rev=${rev} for <nixpkgs> (extracted from flake.nix)"; set +x
rm -f result
nixpkgs="https://github.com/nixos/nixpkgs/archive/${rev}.tar.gz"
nixosconfig="$(pwd)/configuration.nix"
echo
echo ":: Setting NIX_PATH to the same values flakes is using"; set -x
NIX_PATH="nixpkgs=${nixpkgs}:nixos-config=${nixosconfig}" \
nix-build '<nixpkgs/nixos>' -A config.system.build.toplevel
set +x
# nixos-rebuild build
classic_path="$(readlink -f ./result)"
set +x
echo
echo "flake: ${flake_path}"
echo "clssc: ${classic_path}"
if [[ "${flake_path}" != "${classic_path}" ]]; then
exit -1
fi

View file

@ -15,7 +15,6 @@
services.sshd.enable = true;
# etc
networking.hostName = "mysystem";
}

View file

@ -1,14 +1,22 @@
{
description = "An example NixOS configuration";
inputs.nixpkgs = { url = "github:nixos/nixpkgs/nixos-unstable"; };
inputs = {
nixpkgs = { url = "github:nixos/nixpkgs/nixos-unstable"; };
};
outputs = { self, nixpkgs }: {
outputs = inputs:
/* ignore:: */ let ignoreme = ({config,lib,...}: with lib; { system.nixos.revision = mkForce null; system.nixos.versionSuffix = mkForce "pre-git"; }); in
{
nixosConfigurations = {
mysystem = nixpkgs.lib.nixosSystem {
mysystem = inputs.nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
(import ./configuration.nix)
/* ignore */ ignoreme # ignore this; don't include it; it is a small helper for this example
];
};
};

1
nixos-config.nix Normal file
View file

@ -0,0 +1 @@
(builtins.getFlake (toString ./.)).nixosConfigurations.mysystem

1
result Symbolic link
View file

@ -0,0 +1 @@
/nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git