nixos/justfile

241 lines
7.4 KiB
Makefile
Raw Permalink Normal View History

2025-02-18 20:17:57 +01:00
# export NIXPKGS_ALLOW_UNFREE := "1"
2025-02-20 22:50:06 +01:00
nixcmd := "nix --experimental-features 'nix-command flakes'"
@_default:
just --list
2025-02-23 23:04:41 +01:00
[doc("Build a flake output")]
2026-01-21 20:48:12 +01:00
[group('tools')]
build target *ARGS="":
2025-02-24 22:52:19 +01:00
@{{ nixcmd }} run {{ ARGS }} nixpkgs#nix-output-monitor -- build {{ justfile_directory() }}#{{ target }}
2025-02-23 23:04:41 +01:00
[doc('Build a nixos configuration')]
[group('deploy')]
build-system hostname=`hostname -s` *ARGS="":
@just build nixosConfigurations."{{ hostname }}".config.system.build.toplevel {{ ARGS }}
2025-02-23 23:04:41 +01:00
2025-02-20 22:50:06 +01:00
[doc('Wrapper for nixos-facter')]
2025-02-03 07:55:17 +01:00
[group('deploy')]
2025-02-23 23:04:41 +01:00
factor hostname=`hostname -s` target='':
2025-02-18 20:17:57 +01:00
#!/usr/bin/env -S bash -e
target="{{ target }}"
2026-01-21 20:48:12 +01:00
if [ ! -d "hosts/{{ hostname }}" ]; then
echo "Error: Host '{{ hostname }}' does not exist in ./hosts/"
exit 1
fi
2025-02-18 20:17:57 +01:00
if [ -z "$target" ]; then
2025-02-23 23:04:41 +01:00
sudo {{ nixcmd }} run nixpkgs#nixos-facter -- -o hosts/{{ hostname }}/facter.json
2025-02-18 20:17:57 +01:00
else
2025-02-20 22:50:06 +01:00
{{ nixcmd }} run github:nix-community/nixos-anywhere -- \
2025-02-18 20:17:57 +01:00
--flake .#{{ hostname }} \
--target-host {{ target }} \
--generate-hardware-config nixos-facter \
2025-02-20 22:50:06 +01:00
./hosts/{{ hostname }}/facter.json
2025-02-18 20:17:57 +01:00
fi
2025-02-03 07:55:17 +01:00
2025-02-20 22:50:06 +01:00
[doc('Wrapper for nixos-rebuild switch')]
2025-02-19 20:08:25 +00:00
[group("deploy")]
2026-01-21 20:48:12 +01:00
switch hostname=`hostname -s` target='' *ARGS="":
2025-02-20 22:50:06 +01:00
#!/usr/bin/env -S bash -e
target="{{ target }}"
if [ -z "$target" ]; then
2026-01-21 20:48:12 +01:00
sudo {{ nixcmd }} run nixpkgs#nixos-rebuild -- switch --show-trace --flake .#{{ hostname }} {{ ARGS }}
2025-02-20 22:50:06 +01:00
else
{{ nixcmd }} run nixpkgs#nixos-rebuild -- switch \
--flake .#{{ hostname }} \
--use-substitutes \
2025-02-20 22:50:06 +01:00
--target-host {{ target }} \
2026-01-21 20:48:12 +01:00
--sudo {{ ARGS }}
2025-02-20 22:50:06 +01:00
fi
2025-02-19 20:08:25 +00:00
2026-01-21 20:48:12 +01:00
[doc('Wrapper for nixos-rebuild boot')]
[group("deploy")]
boot hostname=`hostname -s` *ARGS="":
sudo {{ nixcmd }} run nixpkgs#nixos-rebuild -- boot --show-trace --flake .#{{ hostname }} {{ ARGS }}
[doc('Wrapper for nixos-rebuild test')]
[group("deploy")]
test hostname=`hostname -s` *ARGS="":
sudo {{ nixcmd }} run nixpkgs#nixos-rebuild -- test --show-trace --flake .#{{ hostname }} {{ ARGS }}
2025-02-20 22:50:06 +01:00
[doc('Use nixos-anywhere to deploy to a remote host')]
2025-02-03 07:55:17 +01:00
[group('deploy')]
2025-03-09 19:19:03 +01:00
deploy-remote hostname target:
2025-02-03 07:55:17 +01:00
#!/usr/bin/env -S bash -e
git add .
2025-02-20 22:50:06 +01:00
temp=$(mktemp -d)
trap "rm -rf $temp" EXIT
install -d -m755 "$temp/etc/ssh"
2025-02-03 07:55:17 +01:00
2025-12-14 01:55:30 +01:00
# Copy ssh key to decrypt agenix secrets
2025-02-20 22:50:06 +01:00
just age -d "./secrets/hosts/{{ hostname }}/id_ed25519.age" > "$temp/etc/ssh/ssh_host_ed25519_key"
chmod 600 "$temp/etc/ssh/ssh_host_ed25519_key"
cp "./secrets/hosts/{{ hostname }}/id_ed25519.pub" "$temp/etc/ssh/ssh_host_ed25519_key.pub"
2025-02-03 07:55:17 +01:00
# Deploy
2025-02-20 22:50:06 +01:00
{{ nixcmd }} run github:nix-community/nixos-anywhere -- \
2025-02-03 07:55:17 +01:00
--flake .#{{ hostname }} \
2025-02-20 22:50:06 +01:00
--disk-encryption-keys /luks-password <(just age -d ./secrets/luks-password.age) \
--extra-files "$temp" \
2025-03-09 20:30:33 +01:00
--target-host "{{ target }}"
2025-02-03 07:55:17 +01:00
2025-02-20 22:50:06 +01:00
[doc('A wrapper disko-install')]
2025-02-03 07:55:17 +01:00
[group('deploy')]
2025-03-09 19:19:03 +01:00
disko-install hostname disk:
2026-01-19 17:44:37 +01:00
sudo {{ nixcmd }} run 'github:nix-community/disko/latest#disko-install' -- --flake .#{{ hostname }} --disk main {{ disk }}
2025-02-03 07:55:17 +01:00
2025-02-20 22:50:06 +01:00
[doc('Build an install ISO for a host')]
2026-01-21 20:48:12 +01:00
[group('tools')]
2025-02-20 22:50:06 +01:00
iso hostname:
{{ nixcmd }} build .#nixosConfigurations.{{ hostname }}.config.formats.install-iso
2025-02-03 07:55:17 +01:00
2025-12-13 19:31:56 +01:00
[doc('Build the bootstrap ISO for USB installation')]
[group('deploy')]
bootstrap-iso:
@echo "Building bootstrap ISO..."
2026-01-19 17:44:37 +01:00
{{ nixcmd }} build .#nixosConfigurations.bootstrap.config.system.build.isoImage
2025-12-13 19:31:56 +01:00
@echo "ISO built: $(ls -1 result/iso/*.iso)"
[doc('Flash the bootstrap ISO to a USB drive')]
[group('deploy')]
bootstrap-flash device:
#!/usr/bin/env -S bash -e
if [ ! -b "{{ device }}" ]; then
echo "Error: {{ device }} is not a block device"
exit 1
fi
2025-12-13 19:41:40 +01:00
2025-12-13 19:31:56 +01:00
# Build the ISO first if needed
if [ ! -d "result/iso" ]; then
just bootstrap-iso
fi
2025-12-13 19:41:40 +01:00
2025-12-13 19:31:56 +01:00
iso_file=$(ls -1 result/iso/*.iso | head -1)
echo "Flashing $iso_file to {{ device }}..."
echo "WARNING: This will ERASE ALL DATA on {{ device }}"
read -p "Are you sure? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
sudo dd if="$iso_file" of="{{ device }}" bs=4M status=progress oflag=sync
echo "Done! You can now boot from {{ device }}"
else
echo "Aborted"
fi
2025-02-20 22:50:06 +01:00
[doc('Runs (r)age with yubikey identity')]
2025-02-03 07:55:17 +01:00
[group('secret')]
2025-02-20 22:50:06 +01:00
age *ARGS="--help":
@{{ nixcmd }} shell nixpkgs#rage nixpkgs#age-plugin-yubikey --command rage {{ ARGS }} -i ./secrets/yubikey-identity.pub
2025-02-03 07:55:17 +01:00
2025-02-20 22:50:06 +01:00
[doc('Decrypt a file to stdout')]
[group('secret')]
decrypt file:
just age -d {{ file }}
2025-02-03 07:55:17 +01:00
2025-02-20 22:50:06 +01:00
[doc('Edit an encrypted file in $EDITOR')]
2025-02-03 07:55:17 +01:00
[group('secret')]
2025-02-20 22:50:06 +01:00
secret-edit name:
{{ nixcmd }} run .#agenix-rekey.x86_64-linux.edit-view edit {{ name }}
2025-02-03 07:55:17 +01:00
2025-02-20 22:50:06 +01:00
[doc('Rekey all secrets - needed when adding secrets/hosts')]
2025-02-03 07:55:17 +01:00
[group('secret')]
secret-rekey:
2025-02-20 22:50:06 +01:00
{{ nixcmd }} run .#agenix-rekey.x86_64-linux.rekey
2025-02-03 07:55:17 +01:00
2025-02-20 22:50:06 +01:00
[doc("Sets up configuration + SSH keys for a new host")]
2026-01-21 20:48:12 +01:00
[group('maintenance')]
2025-02-20 22:50:06 +01:00
new-host hostname username:
#!/usr/bin/env -S bash -e
2026-01-21 20:48:12 +01:00
if [ -d "hosts/{{ hostname }}" ]; then
echo "Error: Host '{{ hostname }}' already exists."
exit 1
fi
2025-02-20 22:50:06 +01:00
temp=$(mktemp -d)
trap "rm -rf $temp" EXIT
echo "Setting up folders"
mkdir -p "secrets/hosts/{{ hostname }}"
mkdir -p "hosts/{{ hostname }}"
2025-12-14 01:55:30 +01:00
cat > "hosts/{{ hostname }}/default.nix" <<'EOF'
{
config.fireproof.hostname = "{{ hostname }}";
config.fireproof.username = "{{ username }}";
imports = [];
}
EOF
2025-02-20 22:50:06 +01:00
echo "Generating SSH key for {{ username }}@{{ hostname }}"
ssh-keygen -q -t ed25519 -f "$temp/id_ed25519" -C "{{ username }}@{{ hostname }}" -N ""
cp "$temp/id_ed25519.pub" "secrets/hosts/{{ hostname }}/id_ed25519.pub"
echo "Encrypting SSH key"
just age -e "$temp/id_ed25519" -o "secrets/hosts/{{ hostname }}/id_ed25519.age"
2026-01-21 20:48:12 +01:00
echo "Secret rekeying..."
just secret-rekey
2025-02-20 22:50:06 +01:00
echo "Remember to update ./hosts/default.nix eg:"
# Bold with no newline
cat <<EOF
2026-01-21 20:48:12 +01:00
{{ BOLD }}{{ hostname }} = mkSystem {host = ./{{ hostname }};};{{ NORMAL }}
2025-02-20 22:50:06 +01:00
EOF
2025-02-23 23:04:41 +01:00
[doc("Update flake.lock")]
2026-01-21 20:48:12 +01:00
[group('maintenance')]
update input='':
{{ nixcmd }} flake update {{ input }}
2025-02-23 23:04:41 +01:00
2026-01-21 20:48:12 +01:00
[doc('Format all files using treefmt')]
[group('maintenance')]
fmt:
{{ nixcmd }} fmt
[doc('Run flake check to validate configuration')]
[group('maintenance')]
check:
{{ nixcmd }} flake check
[doc('Collect garbage and delete old generations')]
[group('maintenance')]
gc days='7':
sudo nix-collect-garbage --delete-older-than {{ days }}d
sudo nix-env -p /nix/var/nix/profiles/system --delete-older-than {{ days }}d
2025-02-23 23:04:41 +01:00
[doc("Run nix-tree")]
[group("tools")]
tree *ARGS=("--derivation .#nixosConfigurations." + shell("hostname -s") + ".config.system.build.toplevel"):
{{ nixcmd }} run github:utdemir/nix-tree -- {{ ARGS }}
[doc("Run nix-diff between current system")]
[group("tools")]
diff hostname=`hostname -s`: (build-system hostname)
2025-02-23 23:04:41 +01:00
{{ nixcmd }} run nixpkgs#nvd -- diff /run/current-system {{ justfile_directory() }}/result
2026-01-21 20:48:12 +01:00
[doc('List system generations')]
[group('tools')]
history:
sudo nix-env -p /nix/var/nix/profiles/system --list-generations
[doc('Open nix repl with flake loaded')]
[group('tools')]
repl:
{{ nixcmd }} repl --expr 'builtins.getFlake "path:{{ justfile_directory() }}"'
2025-02-23 23:04:41 +01:00
[doc("Run nurl")]
[group("tools")]
nurl *ARGS="--help":
{{ nixcmd }} run nixpkgs#nurl -- {{ ARGS }}
2026-01-21 20:48:12 +01:00
[doc('Remove build results and temporary files')]
[group('tools')]
clean:
rm -rf result result-*