feat: add set-nix-channel module to manage nix-channel automatically
This commit is contained in:
40
utils/modules/set-nix-channel.nix
Normal file
40
utils/modules/set-nix-channel.nix
Normal 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."
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user