From a8a6178139b532c494198406110df54216945208 Mon Sep 17 00:00:00 2001 From: Cole Mickens Date: Sun, 25 Oct 2020 03:59:16 -0700 Subject: [PATCH] use a tiny helper module to unify output --- README.md | 54 ++++++++++++++++++++++------------------------- check.sh | 46 ++++++++++++++++++++++++++++++++++++++++ configuration.nix | 3 +-- flake.nix | 16 ++++++++++---- nixos-config.nix | 1 + result | 1 + 6 files changed, 86 insertions(+), 35 deletions(-) create mode 100755 check.sh create mode 100644 nixos-config.nix create mode 120000 result diff --git a/README.md b/README.md index 3995c20..82f8719 100644 --- a/README.md +++ b/README.md @@ -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 - nixos-rebuild build --flake '.#mysystem' - readlink -f result - /nix/store/0imi716z1qd04pfh4zdw6mb0gnxmakjs-nixos-system-nixos-21.03.20201020.007126e - ``` +:: 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 - Note, nixos-rebuild is basically just some magic to build the right derivation - and then set it as a system profile, and activate it. +:: Using rev=007126eef72271480cb7670e19e501a1ad2c1ff2 for (extracted from flake.nix) -2. without flakes: +:: 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 '' -A config.system.build.toplevel +/nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git ++ set +x - ```shell - export NIX_PATH=nixos-config=$(pwd)/configuration.nix:nixpkgs=https://github.com/nixos/nixpkgs/archive/nixos-unstable.tar.gz +flake: /nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git +clssc: /nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git - - /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.) +``` diff --git a/check.sh b/check.sh new file mode 100755 index 0000000..3cf74b9 --- /dev/null +++ b/check.sh @@ -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 (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 '' -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 + diff --git a/configuration.nix b/configuration.nix index 1a6dce6..15401f1 100644 --- a/configuration.nix +++ b/configuration.nix @@ -15,7 +15,6 @@ services.sshd.enable = true; - # etc - + networking.hostName = "mysystem"; } diff --git a/flake.nix b/flake.nix index 1d4ee46..e0a0297 100644 --- a/flake.nix +++ b/flake.nix @@ -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) + (import ./configuration.nix) + + /* ignore */ ignoreme # ignore this; don't include it; it is a small helper for this example ]; }; }; diff --git a/nixos-config.nix b/nixos-config.nix new file mode 100644 index 0000000..81fee72 --- /dev/null +++ b/nixos-config.nix @@ -0,0 +1 @@ +(builtins.getFlake (toString ./.)).nixosConfigurations.mysystem diff --git a/result b/result new file mode 120000 index 0000000..7db43d5 --- /dev/null +++ b/result @@ -0,0 +1 @@ +/nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git \ No newline at end of file