workstation | refactoring around sysrtemd service + new service to remove ~/.keychain on boot

This commit is contained in:
Don Harper 2023-04-26 07:31:29 -05:00
parent a74e640a54
commit 4cd74f8f49
8 changed files with 113 additions and 26 deletions

View file

@ -7,7 +7,7 @@
/home/don/nixos/workstation/configuration.nix
/home/don/nixos/workstation/home
/home/don/nixos/workstation/sway.nix
/home/don/nixos/workstation/detect-reboot-needed.nix
/home/don/nixos/workstation/systemd.nix
];
networking.hostName = "ace";
}

View file

@ -7,7 +7,7 @@
/home/don/nixos/workstation/configuration.nix
/home/don/nixos/workstation/home
/home/don/nixos/workstation/sway.nix
/home/don/nixos/workstation/detect-reboot-needed.nix
/home/don/nixos/workstation/systemd.nix
];
networking.hostName = "dragon";
}

View file

@ -8,7 +8,7 @@
/home/don/nixos/workstation/configuration.nix
/home/don/nixos/workstation/home
/home/don/nixos/workstation/sway.nix
/home/don/nixos/workstation/detect-reboot-needed.nix
/home/don/nixos/workstation/systemd.nix
];
networking.hostName = "eve";
}

View file

@ -9,7 +9,7 @@
/home/don/nixos/loki/kernel.nix
/home/don/nixos/workstation/home
/home/don/nixos/workstation/sway.nix
/home/don/nixos/workstation/detect-reboot-needed.nix
/home/don/nixos/workstation/systemd.nix
];
networking.hostName = "loki";
}

View file

@ -7,7 +7,7 @@
/home/don/nixos/workstation/configuration.nix
/home/don/nixos/workstation/home
/home/don/nixos/workstation/sway.nix
/home/don/nixos/workstation/detect-reboot-needed.nix
/home/don/nixos/workstation/systemd.nix
];
networking.hostName = "pocket2";
}

View file

@ -8,7 +8,7 @@
/home/don/nixos/workstation/configuration.nix
/home/don/nixos/workstation/home
/home/don/nixos/workstation/sway.nix
/home/don/nixos/workstation/detect-reboot-needed.nix
/home/don/nixos/workstation/systemd.nix
];
networking.hostName = "smaug";
}

View file

@ -245,34 +245,46 @@ in
lidSwitchExternalPower = "ignore";
lidSwitchDocked = "ignore";
};
systemd.services.tailscale-autoconnect = {
description = "Automatic connection to Tailscale";
systemd.services = {
tailscale-autoconnect = {
description = "Automatic connection to Tailscale";
# make sure tailscale is running before trying to connect to tailscale
after = [ "network-pre.target" "tailscale.service" ];
wants = [ "network-pre.target" "tailscale.service" ];
wantedBy = [ "multi-user.target" ];
# make sure tailscale is running before trying to connect to tailscale
after = [ "network-pre.target" "tailscale.service" ];
wants = [ "network-pre.target" "tailscale.service" ];
wantedBy = [ "multi-user.target" ];
# set this service as a oneshot job
serviceConfig.Type = "oneshot";
# set this service as a oneshot job
serviceConfig.Type = "oneshot";
# have the job run this shell script
script = with pkgs; ''
# wait for tailscaled to settle
sleep 2
# have the job run this shell script
script = with pkgs; ''
# wait for tailscaled to settle
sleep 2
# check if we are already authenticated to tailscale
status="$(${tailscale}/bin/tailscale status -json | ${jq}/bin/jq -r .BackendState)"
if [ $status = "Running" ]; then # if so, then do nothing
exit 0
fi
# check if we are already authenticated to tailscale
status="$(${tailscale}/bin/tailscale status -json | ${jq}/bin/jq -r .BackendState)"
if [ $status = "Running" ]; then # if so, then do nothing
exit 0
fi
# otherwise authenticate with tailscale
${tailscale}/bin/tailscale up --operator=don --authkey tskey-api-kDQcva6CNTRL-kvcJzSix6yLb2dgjr1Pi
'';
# otherwise authenticate with tailscale
${tailscale}/bin/tailscale up --operator=don --authkey tskey-api-kDQcva6CNTRL-kvcJzSix6yLb2dgjr1Pi
'';
};
clean-keychain = {
description = "Clean up .keychain on boot";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.coreutils-full}/bin/rm -rf /home/don/.keychain";
};
};
};
# Enable the OpenSSH daemon.
services.openssh = {
enable = true;

75
workstation/systemd.nix Normal file
View file

@ -0,0 +1,75 @@
{ pkgs, ... }:
let
readlink = "${pkgs.coreutils}/bin/readlink";
notify-send = "${pkgs.libnotify}/bin/notify-send";
in {
systemd = {
services = {
tailscale-autoconnect = {
description = "Automatic connection to Tailscale";
# make sure tailscale is running before trying to connect to tailscale
after = [ "network-pre.target" "tailscale.service" ];
wants = [ "network-pre.target" "tailscale.service" ];
wantedBy = [ "multi-user.target" ];
# set this service as a oneshot job
serviceConfig.Type = "oneshot";
# have the job run this shell script
script = with pkgs; ''
# wait for tailscaled to settle
sleep 2
# check if we are already authenticated to tailscale
status="$(${tailscale}/bin/tailscale status -json | ${jq}/bin/jq -r .BackendState)"
if [ $status = "Running" ]; then # if so, then do nothing
exit 0
fi
# otherwise authenticate with tailscale
${tailscale}/bin/tailscale up --operator=don --authkey tskey-api-kDQcva6CNTRL-kvcJzSix6yLb2dgjr1Pi
'';
};
clean-keychain = {
description = "Clean up .keychain on boot";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.coreutils-full}/bin/rm -rf /home/don/.keychain";
};
};
};
user = {
services = {
detect-reboot-for-upgrade = {
script = ''
set -eu -o pipefail
booted="$(${readlink} /run/booted-system/{initrd,kernel,kernel-modules})"
built="$(${readlink} /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})"
if [[ "''${booted}" != "''${built}" ]];
then
echo "Looks like we need a reboot!"
${notify-send} --urgency=low --icon=system-reboot "Reboot is needed for a NixOS upgrade."
fi
'';
serviceConfig = {
Type = "oneshot";
};
};
};
timers = {
detect-reboot-for-upgrade = {
wantedBy = [ "timers.target" ];
partOf = [ "detect-reboot-for-upgrade.service" ];
timerConfig = {
OnCalendar = "hourly";
Unit = "detect-reboot-for-upgrade.service";
};
};
};
};
};
}