feat: add set-nix-channel module to manage nix-channel automatically

This commit is contained in:
2025-05-29 00:38:12 +02:00
parent c0d51ee06d
commit cf340ca277
9 changed files with 48 additions and 0 deletions

View File

@@ -64,6 +64,7 @@
# setup network # setup network
./modules/setupnetwork.nix ./modules/setupnetwork.nix
./modules/set-nix-channel.nix # Automatically manage nix-channel from /var/bento/channel
./hardware-configuration.nix ./hardware-configuration.nix

View File

@@ -0,0 +1 @@
../../../utils/modules/set-nix-channel.nix

View File

@@ -15,6 +15,7 @@
./utils/modules/promtail ./utils/modules/promtail
./utils/modules/victoriametrics ./utils/modules/victoriametrics
./utils/modules/netdata.nix ./utils/modules/netdata.nix
./modules/set-nix-channel.nix # Automatically manage nix-channel from /var/bento/channel
./hardware-configuration.nix ./hardware-configuration.nix
]; ];

View File

@@ -0,0 +1 @@
../../../utils/modules/set-nix-channel.nix

View File

@@ -42,6 +42,7 @@ in {
# ./modules/steam.nix # ./modules/steam.nix
./modules/fingerprint.nix ./modules/fingerprint.nix
./modules/set-nix-channel.nix # Automatically manage nix-channel from /var/bento/channel
./hardware-configuration.nix ./hardware-configuration.nix

View File

@@ -0,0 +1 @@
../../../utils/modules/set-nix-channel.nix

View File

@@ -23,6 +23,7 @@
./utils/modules/promtail ./utils/modules/promtail
./utils/modules/borgbackup.nix ./utils/modules/borgbackup.nix
./utils/modules/netdata.nix ./utils/modules/netdata.nix
./modules/set-nix-channel.nix # Automatically manage nix-channel from /var/bento/channel
./hardware-configuration.nix ./hardware-configuration.nix

View File

@@ -0,0 +1 @@
../../../utils/modules/set-nix-channel.nix

View File

@@ -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."
'';
};
};
}