mirror of
https://github.com/SebastianStork/nixos-config.git
synced 2026-01-21 15:11:34 +01:00
Refactor install-anywhere script
This commit is contained in:
parent
225574cbbf
commit
93eb233b14
3 changed files with 30 additions and 22 deletions
|
|
@ -2,8 +2,8 @@ _: {
|
||||||
perSystem =
|
perSystem =
|
||||||
{ pkgs, ... }:
|
{ pkgs, ... }:
|
||||||
{
|
{
|
||||||
packages.provision-keys = pkgs.writeShellApplication {
|
packages.install-anywhere = pkgs.writeShellApplication {
|
||||||
name = "provision-keys";
|
name = "install-anywhere";
|
||||||
|
|
||||||
runtimeInputs = [
|
runtimeInputs = [
|
||||||
pkgs.sops
|
pkgs.sops
|
||||||
|
|
@ -16,34 +16,43 @@ _: {
|
||||||
excludeShellChecks = [ "SC2155" ];
|
excludeShellChecks = [ "SC2155" ];
|
||||||
|
|
||||||
text = ''
|
text = ''
|
||||||
if [[ $# -ne 1 ]]; then
|
if [[ $# -ne 2 ]]; then
|
||||||
echo "Usage: $0 <host>"
|
echo "Usage: $0 <host> <destination>"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
host="$1"
|
host="$1"
|
||||||
|
destination="$2"
|
||||||
impermanence=$(nixos-option --flake ".#$host" custom.impermanence.enable | awk '/^Value:/ {getline; print $1}')
|
|
||||||
|
|
||||||
if [ "$impermanence" = "true" ]; then
|
|
||||||
root="/tmp/anywhere/$host/persist"
|
|
||||||
else
|
|
||||||
root="/tmp/anywhere/$host"
|
root="/tmp/anywhere/$host"
|
||||||
|
|
||||||
|
impermanence=$(nix eval ".#nixosConfigurations.$host.config.custom.impermanence.enable")
|
||||||
|
if [ "$impermanence" = true ]; then
|
||||||
|
ssh_dir="$root/persist/etc/ssh"
|
||||||
|
else
|
||||||
|
ssh_dir="$root/etc/ssh"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
mkdir --parents "$root/etc/ssh"
|
if [ ! -f "$ssh_dir/ssh_host_ed25519_key" ]; then
|
||||||
ssh-keygen -C "root@$host" -f "$root/etc/ssh/ssh_host_ed25519_key" -N "" -q
|
echo "==> Generating new SSH host keys..."
|
||||||
|
mkdir --parents "$ssh_dir"
|
||||||
|
ssh-keygen -C "root@$host" -f "$ssh_dir/ssh_host_ed25519_key" -N "" -q
|
||||||
|
|
||||||
new_age_key=$(ssh-to-age -i "$root/etc/ssh/ssh_host_ed25519_key.pub")
|
echo "==> Replacing old age key with new age key..."
|
||||||
|
new_age_key=$(ssh-to-age -i "$ssh_dir/ssh_host_ed25519_key.pub")
|
||||||
# Replace old age key with new age key
|
|
||||||
sed -i -E "s|(agePublicKey\s*=\s*\")[^\"]*(\";)|\1$new_age_key\2|" "hosts/$host/default.nix"
|
sed -i -E "s|(agePublicKey\s*=\s*\")[^\"]*(\";)|\1$new_age_key\2|" "hosts/$host/default.nix"
|
||||||
|
|
||||||
export BW_SESSION=$(bw login | awk -F'"' '/export BW_SESSION/ {print $2}')
|
echo "==> Updating SOPS secrets..."
|
||||||
|
export BW_SESSION=$(bw login --raw)
|
||||||
export SOPS_AGE_KEY=$(bw get item 'admin age-key' | jq -r '.notes')
|
export SOPS_AGE_KEY=$(bw get item 'admin age-key' | jq -r '.notes')
|
||||||
export SOPS_CONFIG=$(nix build .#sops-config --print-out-paths)
|
export SOPS_CONFIG=$(nix build .#sops-config --print-out-paths)
|
||||||
|
|
||||||
sops updatekeys --yes "hosts/$host/secrets.json"
|
sops updatekeys --yes "hosts/$host/secrets.json"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "==> Installing system..."
|
||||||
|
nix run github:nix-community/nixos-anywhere -- \
|
||||||
|
--extra-files "$root" \
|
||||||
|
--flake ".#$host" \
|
||||||
|
--target-host "$destination"
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@
|
||||||
pkgs.jq
|
pkgs.jq
|
||||||
];
|
];
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
export BW_SESSION=$(bw login | awk -F'"' '/export BW_SESSION/ {print $2}')
|
export BW_SESSION=$(bw login --raw)
|
||||||
export SOPS_AGE_KEY=$(bw get item 'admin age-key' | jq -r '.notes')
|
export SOPS_AGE_KEY=$(bw get item 'admin age-key' | jq -r '.notes')
|
||||||
export SOPS_CONFIG=${self'.packages.sops-config}
|
export SOPS_CONFIG=${self'.packages.sops-config}
|
||||||
'';
|
'';
|
||||||
|
|
|
||||||
3
justfile
3
justfile
|
|
@ -20,8 +20,7 @@ deploy +hosts:
|
||||||
deploy --skip-checks --targets $(echo {{ hosts }} | sed 's/[^ ]*/\.#&/g')
|
deploy --skip-checks --targets $(echo {{ hosts }} | sed 's/[^ ]*/\.#&/g')
|
||||||
|
|
||||||
install host destination='root@installer':
|
install host destination='root@installer':
|
||||||
nix run .#provision-keys -- {{ host }}
|
nix run .#install-anywhere -- {{ host }} {{ destination }}
|
||||||
nix run github:nix-community/nixos-anywhere -- --extra-files /tmp/anywhere/{{ host }} --flake .#{{ host }} --target-host {{ destination }}
|
|
||||||
|
|
||||||
repair:
|
repair:
|
||||||
nix-store --verify --check-contents --repair
|
nix-store --verify --check-contents --repair
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue