From cf340ca2774e3dd11e7f79ad38ae561ccb13ff58 Mon Sep 17 00:00:00 2001 From: Dominik Polakovics Date: Thu, 29 May 2025 00:38:12 +0200 Subject: [PATCH] feat: add set-nix-channel module to manage nix-channel automatically --- hosts/fw/configuration.nix | 1 + hosts/fw/modules/set-nix-channel.nix | 1 + hosts/mail/configuration.nix | 1 + hosts/mail/modules/set-nix-channel.nix | 1 + hosts/nb/configuration.nix | 1 + hosts/nb/modules/set-nix-channel.nix | 1 + hosts/web-arm/configuration.nix | 1 + hosts/web-arm/modules/set-nix-channel.nix | 1 + utils/modules/set-nix-channel.nix | 40 +++++++++++++++++++++++ 9 files changed, 48 insertions(+) create mode 120000 hosts/fw/modules/set-nix-channel.nix create mode 120000 hosts/mail/modules/set-nix-channel.nix create mode 120000 hosts/nb/modules/set-nix-channel.nix create mode 120000 hosts/web-arm/modules/set-nix-channel.nix create mode 100644 utils/modules/set-nix-channel.nix diff --git a/hosts/fw/configuration.nix b/hosts/fw/configuration.nix index 0401028..5bd0338 100644 --- a/hosts/fw/configuration.nix +++ b/hosts/fw/configuration.nix @@ -64,6 +64,7 @@ # setup network ./modules/setupnetwork.nix + ./modules/set-nix-channel.nix # Automatically manage nix-channel from /var/bento/channel ./hardware-configuration.nix diff --git a/hosts/fw/modules/set-nix-channel.nix b/hosts/fw/modules/set-nix-channel.nix new file mode 120000 index 0000000..4fdabfe --- /dev/null +++ b/hosts/fw/modules/set-nix-channel.nix @@ -0,0 +1 @@ +../../../utils/modules/set-nix-channel.nix \ No newline at end of file diff --git a/hosts/mail/configuration.nix b/hosts/mail/configuration.nix index 91c0004..0bc9d7b 100644 --- a/hosts/mail/configuration.nix +++ b/hosts/mail/configuration.nix @@ -15,6 +15,7 @@ ./utils/modules/promtail ./utils/modules/victoriametrics ./utils/modules/netdata.nix + ./modules/set-nix-channel.nix # Automatically manage nix-channel from /var/bento/channel ./hardware-configuration.nix ]; diff --git a/hosts/mail/modules/set-nix-channel.nix b/hosts/mail/modules/set-nix-channel.nix new file mode 120000 index 0000000..4fdabfe --- /dev/null +++ b/hosts/mail/modules/set-nix-channel.nix @@ -0,0 +1 @@ +../../../utils/modules/set-nix-channel.nix \ No newline at end of file diff --git a/hosts/nb/configuration.nix b/hosts/nb/configuration.nix index 6d6ec60..6407546 100644 --- a/hosts/nb/configuration.nix +++ b/hosts/nb/configuration.nix @@ -42,6 +42,7 @@ in { # ./modules/steam.nix ./modules/fingerprint.nix + ./modules/set-nix-channel.nix # Automatically manage nix-channel from /var/bento/channel ./hardware-configuration.nix diff --git a/hosts/nb/modules/set-nix-channel.nix b/hosts/nb/modules/set-nix-channel.nix new file mode 120000 index 0000000..4fdabfe --- /dev/null +++ b/hosts/nb/modules/set-nix-channel.nix @@ -0,0 +1 @@ +../../../utils/modules/set-nix-channel.nix \ No newline at end of file diff --git a/hosts/web-arm/configuration.nix b/hosts/web-arm/configuration.nix index 44de2e2..ac3d270 100644 --- a/hosts/web-arm/configuration.nix +++ b/hosts/web-arm/configuration.nix @@ -23,6 +23,7 @@ ./utils/modules/promtail ./utils/modules/borgbackup.nix ./utils/modules/netdata.nix + ./modules/set-nix-channel.nix # Automatically manage nix-channel from /var/bento/channel ./hardware-configuration.nix diff --git a/hosts/web-arm/modules/set-nix-channel.nix b/hosts/web-arm/modules/set-nix-channel.nix new file mode 120000 index 0000000..4fdabfe --- /dev/null +++ b/hosts/web-arm/modules/set-nix-channel.nix @@ -0,0 +1 @@ +../../../utils/modules/set-nix-channel.nix \ No newline at end of file diff --git a/utils/modules/set-nix-channel.nix b/utils/modules/set-nix-channel.nix new file mode 100644 index 0000000..22823ef --- /dev/null +++ b/utils/modules/set-nix-channel.nix @@ -0,0 +1,40 @@ +# utils/modules/set-nix-channel.nix +{ config, pkgs, ... }: + +{ + systemd.services.set-nix-channel = { + description = "Set nix-channel from /var/bento/channel"; + after = [ "network.target" ]; # Ensure network is up + wantedBy = [ "multi-user.target" ]; # Start on boot + + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; # Consider the service "active" after successful run + User = "root"; + ExecStart = pkgs.writeShellScript "set-nix-channel-script" '' + set -e # Exit immediately if a command exits with a non-zero status. + CHANNEL_FILE="/var/bento/channel" + CHANNEL_NAME="nixos" + + if [ ! -f "$CHANNEL_FILE" ]; then + echo "Error: Channel file $CHANNEL_FILE not found." >&2 + exit 1 + fi + + CHANNEL_URL=$(cat "$CHANNEL_FILE") + + if [ -z "$CHANNEL_URL" ]; then + echo "Error: Channel file $CHANNEL_FILE is empty." >&2 + exit 1 + fi + + # Attempt to remove existing channel to ensure clean state, ignore error if it doesn't exist + ${pkgs.nix}/bin/nix-channel --remove "$CHANNEL_NAME" || true + ${pkgs.nix}/bin/nix-channel --add "$CHANNEL_URL" "$CHANNEL_NAME" + ${pkgs.nix}/bin/nix-channel --update "$CHANNEL_NAME" + + echo "Successfully set and updated channel '$CHANNEL_NAME' from $CHANNEL_FILE." + ''; + }; + }; +} \ No newline at end of file