nixos/justfile

194 lines
5.6 KiB
Makefile
Raw 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-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-20 22:50:06 +01:00
{{ nixcmd }} 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
2025-02-23 23:04:41 +01:00
[doc("Build a flake output")]
build target='':
@{{ nixcmd }} run nixpkgs#nix-output-monitor -- build {{ justfile_directory() }}#{{ target }}
[doc('Build a nixos configuration')]
[group('deploy')]
build-system hostname:
@just build nixosConfigurations."{{ hostname }}".config.system.build.toplevel
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 }}"
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")]
2025-02-23 23:04:41 +01:00
switch hostname=`hostname -s` target='': (build-system hostname)
2025-02-20 22:50:06 +01:00
#!/usr/bin/env -S bash -e
target="{{ target }}"
if [ -z "$target" ]; then
2025-02-23 23:04:41 +01:00
sudo {{ nixcmd }} run nixpkgs#nixos-rebuild -- switch --fast --flake .#{{ hostname }}
2025-02-20 22:50:06 +01:00
else
{{ nixcmd }} run nixpkgs#nixos-rebuild -- switch \
--flake .#{{ hostname }} \
--target-host {{ target }} \
--use-remote-sudo
fi
2025-02-19 20:08:25 +00:00
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-02-23 23:04:41 +01:00
deploy-remote hostname target: (build-system hostname)
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-02-20 22:50:06 +01:00
# Copy ssh key to decrypt agenix secrets
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" \
--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-02-23 23:04:41 +01:00
disko-install hostname disk: (build-system hostname)
2025-02-20 22:50:06 +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')]
2025-02-18 20:17:57 +01:00
[group('deploy')]
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-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 {{ 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")]
new-host hostname username:
#!/usr/bin/env -S bash -e
temp=$(mktemp -d)
trap "rm -rf $temp" EXIT
echo "Setting up folders"
mkdir -p "secrets/hosts/{{ hostname }}"
mkdir -p "hosts/{{ hostname }}"
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"
echo "Remember to update ./hosts/default.nix eg:"
# Bold with no newline
cat <<EOF
{{ BOLD }}{{ hostname }} = mkSystem {
hostname = "{{ hostname }}";
username = "{{ username }}";
modules = [
../modules/required.nix
../modules/shell.nix
../modules/graphical.nix
../modules/devenv.nix
];
};
EOF
2025-02-23 23:04:41 +01:00
[doc("Update flake.lock")]
update:
{{ nixcmd }} flake update
[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: build
{{ nixcmd }} run nixpkgs#nvd -- diff /run/current-system {{ justfile_directory() }}/result
[doc("Run nurl")]
[group("tools")]
nurl *ARGS="--help":
{{ nixcmd }} run nixpkgs#nurl -- {{ ARGS }}