nixos/justfile

138 lines
3.7 KiB
Makefile
Raw Normal View History

2025-02-18 20:17:57 +01:00
# export NIXPKGS_ALLOW_UNFREE := "1"
2025-02-03 07:55:17 +01:00
[group('vm')]
vm-build:
git add .
2025-01-31 15:13:22 +01:00
echo "Building VM..."
2025-02-03 07:55:17 +01:00
nix build .#vm
2025-01-31 15:13:22 +01:00
echo "VM built."
2025-02-03 07:55:17 +01:00
sudo chmod 777 result/nixos.qcow2
2025-01-31 15:13:22 +01:00
echo "VM permissions set."
2025-02-03 07:55:17 +01:00
[group('vm')]
vm-reload:
2025-01-31 15:13:22 +01:00
echo "Reloading VM..."
2025-02-03 07:55:17 +01:00
virsh destroy nixos
2025-01-31 15:13:22 +01:00
virsh start nixos
echo "VM reloaded."
2025-02-03 07:55:17 +01:00
[group('vm')]
vm-switch: vm-build vm-reload
[group('vm')]
vm-init:
2025-01-31 15:13:22 +01:00
virsh pool-define-as nixos dir - - - - $HOME/.local/libvirt/images/nixos
virsh pool-build nixos
virsh pool-start nixos
virt-install \
--name nixos \
--os-variant=nixos-24.05 \
--memory 8192 \
--vcpus=4,maxvcpus=8 \
--cpu host \
--disk result/nixos.qcow2 \
--network user \
--virt-type kvm \
--import \
--graphics spice
2025-02-03 07:55:17 +01:00
[group('vm')]
vm-destroy:
virsh destroy nixos
virsh pool-destroy nixos
virsh pool-undefine nixos
[group('nix')]
repl:
nix repl --show-trace ".#" nixpkgs
[group('deploy')]
2025-02-18 20:17:57 +01:00
deploy-factor hostname target='':
#!/usr/bin/env -S bash -e
target="{{ target }}"
if [ -z "$target" ]; then
sudo nix run nixpkgs#nixos-facter -- -o parts/hosts/{{ hostname }}/facter.json
else
nix run github:nix-community/nixos-anywhere -- \
--flake .#{{ hostname }} \
--target-host {{ target }} \
--generate-hardware-config nixos-facter \
./parts/hosts/{{ hostname }}/facter.json
fi
2025-02-03 07:55:17 +01:00
tmp_dir := "/tmp/secrets/" + uuid()
2025-02-19 20:08:25 +00:00
[group("deploy")]
deploy hostname *ARGS:
nix run nixpkgs#nixos-rebuild -- \
--flake .#{{ hostname }} \
{{ ARGS }} switch
2025-02-03 07:55:17 +01:00
[group('deploy')]
2025-02-19 20:08:25 +00:00
deploy-remote hostname target:
2025-02-03 07:55:17 +01:00
#!/usr/bin/env -S bash -e
git add .
2025-02-18 20:17:57 +01:00
trap "rm -rf {{ tmp_dir }}" EXIT
2025-02-03 07:55:17 +01:00
2025-02-18 20:17:57 +01:00
# Copy ssh key to decrypt agenix secrets
install -d -m755 {{ tmp_dir }}/etc/ssh
2025-02-03 07:55:17 +01:00
just secret-echo ./secrets/hosts/{{ hostname }}/id_ed25519 > {{ tmp_dir }}/etc/ssh/ssh_host_ed25519_key
2025-02-18 20:17:57 +01:00
chmod 600 {{ tmp_dir }}/etc/ssh/ssh_host_ed25519_key
2025-02-03 07:55:17 +01:00
cp ./secrets/hosts/{{ hostname }}/id_ed25519.pub {{ tmp_dir }}/etc/ssh/ssh_host_ed25519_key.pub
# Deploy
nix run github:nix-community/nixos-anywhere -- \
--flake .#{{ hostname }} \
2025-02-18 20:17:57 +01:00
--disk-encryption-keys /luks-password <(just secret-echo ./secrets/luks-password) \
2025-02-03 07:55:17 +01:00
--extra-files {{ tmp_dir }} \
--target-host {{ target }}
[group('deploy')]
2025-02-18 20:17:57 +01:00
deploy-switch hostname target *ARGS:
2025-02-03 07:55:17 +01:00
nix run nixpkgs#nixos-rebuild -- \
--flake .#{{ hostname }} \
--target-host {{ target }} \
--use-remote-sudo \
2025-02-18 20:17:57 +01:00
{{ ARGS }} switch
2025-02-03 07:55:17 +01:00
2025-02-18 20:17:57 +01:00
[group('deploy')]
deploy-iso hostname:
nix build .#nixosConfigurations.{{ hostname }}.config.formats.install-iso
identifier := "./secrets/yubikey-identity.pub"
2025-02-03 07:55:17 +01:00
[group("secret")]
secret-import path:
#!/usr/bin/env bash
# load the file from the root system
cat {{ path }} | nix develop --quiet --command bash -c \
"rage -e -r -o secrets/{{ path }}.age -i {{ identifier }}"
[group('secret')]
secret-echo file:
nix develop --quiet --command bash -c \
"rage -d {{ file }}.age -i {{ identifier }}"
default := ""
[group('secret')]
secret-edit name=default:
nix run .#agenix-rekey.x86_64-linux.edit {{ name }}
[group('secret')]
secret-rekey:
nix develop --quiet --command bash -c \
"agenix rekey"
2025-02-18 20:17:57 +01:00
git add .
2025-02-03 07:55:17 +01:00
[group('secret')]
secret-new-ssh-key hostname $USER:
#!/usr/bin/env -S nix develop --quiet --command bash
mkdir -p secrets/hosts/{{ hostname }}
ssh-keygen -t ed25519 -f secrets/hosts/{{ hostname }}/id_ed25519 -C "${USER}@{{ hostname }}"
age-plugin-yubikey -e secrets/hosts/{{ hostname }}/id_ed25519 \
-o secrets/hosts/{{ hostname }}/id_ed25519.age
rm secrets/hosts/{{ hostname }}/id_ed25519