From 940fb56e7e450b7da56cc333d17ab0edb4cd1d0f Mon Sep 17 00:00:00 2001
From: Martin Fischer <martin@push-f.com>
Date: Sat, 26 Apr 2025 08:26:08 +0200
Subject: refactor: move channel metadata to separate .toml files

---
 nixos/README.md                 |  3 ++-
 nixos/hosts/ev/default.nix      |  1 -
 nixos/hosts/ev/metadata.toml    |  1 +
 nixos/hosts/hamac/default.nix   |  1 -
 nixos/hosts/hamac/metadata.toml |  1 +
 nixos/hosts/tente/default.nix   |  1 -
 nixos/hosts/tente/metadata.toml |  1 +
 nixos/rebuild                   | 15 +++++++--------
 8 files changed, 12 insertions(+), 12 deletions(-)
 create mode 100644 nixos/hosts/ev/metadata.toml
 create mode 100644 nixos/hosts/hamac/metadata.toml
 create mode 100644 nixos/hosts/tente/metadata.toml

(limited to 'nixos')

diff --git a/nixos/README.md b/nixos/README.md
index 67b6cf7..075f812 100644
--- a/nixos/README.md
+++ b/nixos/README.md
@@ -32,7 +32,8 @@ We're assuming that you just installed NixOS by going through the [official inst
      nixos-generate-config --dir .
      ```
 3. Rename the `configuration.nix` to `default.nix`.
-   Add `# channel="..."` to the start of the `default.nix` file where `...` is the key of a channel pinned in `npins/sources.json`.
+   Create a `metadata.toml` file with a `channel` field
+   where the value is the key of a channel pinned in `npins/sources.json`.
    A new channel can be pinned with:
 
    ```
diff --git a/nixos/hosts/ev/default.nix b/nixos/hosts/ev/default.nix
index e59ec01..9842f4f 100644
--- a/nixos/hosts/ev/default.nix
+++ b/nixos/hosts/ev/default.nix
@@ -1,4 +1,3 @@
-# channel="nixos-small"
 # Edit this configuration file to define what should be installed on
 # your system.  Help is available in the configuration.nix(5) man page
 # and in the NixOS manual (accessible by running ‘nixos-help’).
diff --git a/nixos/hosts/ev/metadata.toml b/nixos/hosts/ev/metadata.toml
new file mode 100644
index 0000000..58f0301
--- /dev/null
+++ b/nixos/hosts/ev/metadata.toml
@@ -0,0 +1 @@
+channel = "nixos-small"
diff --git a/nixos/hosts/hamac/default.nix b/nixos/hosts/hamac/default.nix
index 7085ed3..08caa51 100644
--- a/nixos/hosts/hamac/default.nix
+++ b/nixos/hosts/hamac/default.nix
@@ -1,4 +1,3 @@
-# channel="nixos"
 # See the configuration.nix(5) man page and the NixOS manual (accessible by running `nixos-help`).
 
 let
diff --git a/nixos/hosts/hamac/metadata.toml b/nixos/hosts/hamac/metadata.toml
new file mode 100644
index 0000000..fc34e27
--- /dev/null
+++ b/nixos/hosts/hamac/metadata.toml
@@ -0,0 +1 @@
+channel = "nixos"
diff --git a/nixos/hosts/tente/default.nix b/nixos/hosts/tente/default.nix
index 7ecb793..638b8d9 100644
--- a/nixos/hosts/tente/default.nix
+++ b/nixos/hosts/tente/default.nix
@@ -1,4 +1,3 @@
-# channel="nixos-small"
 # Edit this configuration file to define what should be installed on
 # your system. Help is available in the configuration.nix(5) man page, on
 # https://search.nixos.org/options and in the NixOS manual (`nixos-help`).
diff --git a/nixos/hosts/tente/metadata.toml b/nixos/hosts/tente/metadata.toml
new file mode 100644
index 0000000..58f0301
--- /dev/null
+++ b/nixos/hosts/tente/metadata.toml
@@ -0,0 +1 @@
+channel = "nixos-small"
diff --git a/nixos/rebuild b/nixos/rebuild
index 80a52cd..cfc14cf 100755
--- a/nixos/rebuild
+++ b/nixos/rebuild
@@ -11,19 +11,18 @@ if [ ! -f $configPath ]; then
     exit 1
 fi
 
-firstLine=$(head -n1 "$configPath")
-
-if ! echo "$firstLine" | grep -E ^"# channel *=" > /dev/null; then
-    echo "aborting: $configPath doesn't start with \`# channel=\"...\"\`, where ... is a pin from sources.json"
-    exit 1
-fi
-
 nix-eval() {
+    # FUTURE: use --raw when upgrading to NixOS 25.05
     nix-instantiate --eval --read-write-mode "$@" \
       | tr -d \"  # nix-instantiate has no raw output yet (like the experimental nix eval)
 }
 
-channel=$(nix-eval --argstr line "$(echo $firstLine | tr -d \#)" --expr '{line}: (builtins.fromTOML line).channel')
+channel=$(nix-eval --argstr host "$HOSTNAME" --expr '{host}:
+let
+  toml = builtins.readFile ./hosts/${host}/metadata.toml;
+in
+  (builtins.fromTOML toml).channel
+')
 nixpkgsPath=$(nix-eval --argstr channel "$channel" --expr '{channel}: (import ./npins).${channel}.outPath')
 
 # nixos-rebuild always reads Nixpkgs from the NIX_PATH,
-- 
cgit v1.2.3