]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/courge/Makefile
nix: use builtins.path to avoid changes when inputs.self changes
[julm/julm-nix.git] / hosts / courge / Makefile
1 #cwd := $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
2 hostName := courge
3 disk_ssd1 := /dev/disk/by-id/nvme-INTEL_SSDPEKKF256G8L_BTHP91220R5E256B
4 zpool := $(hostName)
5 cipher := aes-128-gcm
6 autotrim := on
7 reservation := 1G
8 compression := zstd
9 TARGET ?= root@192.168.3.101
10
11 machine-id.clear:
12 touch -a $@
13 sudo unshare --mount sh -xc 'mount --bind $@ /etc/machine-id && systemd-machine-id-setup'
14 credential.secret: machine-id.clear
15 sudo unshare --mount sh -xc 'mount --bind machine-id.clear /etc/machine-id && mount --bind . /var/lib/systemd && systemd-creds setup'
16 credential.secret.gpg: credential.secret
17 sudo chown $(USER) credential.secret
18 gpg --encrypt $(shell printf -- ' -r %s' $$(cat .gpg-id)) credential.secret
19 shred -fu $<
20
21 ssh/host.key ssh/host.key.pub:
22 mkdir -p $(@D)
23 ssh-keygen -t ed25519 -f $@
24 ssh/host.key.gpg: ssh/host.key
25 gpg --encrypt $(shell printf -- ' -r %s' $$(cat .gpg-id)) $<
26 shred -fu $<
27 ssh/host.key.cred: ssh/host.key.gpg
28 ../gpg2cred-local.sh $< host.key
29
30 wipe:
31 ssh $(TARGET) sh -xec '" \
32 modprobe zfs; \
33 ! zpool list $(zpool) || zpool export -f $(zpool); \
34 zpool labelclear -f /dev/disk/by-partlabel/$(hostName)_ssd1_zpool || true; \
35 sgdisk --zap-all $(disk_ssd1); \
36 partprobe || true; \
37 udevadm settle; \
38 "'
39
40 part: wipe
41 # https://wiki.archlinux.org/index.php/BIOS_boot_partition
42 #sudo $$(which sgdisk) -a1 -n0:34:2047 -t0:EF02 -c0:"$(hostName)_ssd1_bios" $(disk_ssd1)
43 # https://wiki.archlinux.org/index.php/Partitioning#Tricking_old_BIOS_into_booting_from_GPT
44 #printf '\200\0\0\0\0\0\0\0\0\0\0\0\001\0\0\0' | sudo dd of=$(disk_ssd1) bs=1 seek=462
45 ssh $(TARGET) sh -xec '" \
46 sgdisk -n0::+256M -t0:EF00 -c0:"$(hostName)_ssd1_boot" $(disk_ssd1); \
47 sgdisk -n0:0:+4G -t0:8200 -c0:"$(hostName)_ssd1_swap" $(disk_ssd1); \
48 sgdisk -n0:0:0 -t0:BF01 -c0:"$(hostName)_ssd1_zpool" $(disk_ssd1); \
49 sgdisk --randomize-guids $(disk_ssd1); \
50 sgdisk --backup=$(hostName)_ssd1.sgdisk $(disk_ssd1); \
51 partprobe || true; \
52 udevadm settle; \
53 mkfs.vfat -F 32 -s 1 -n EFI /dev/disk/by-partlabel/$(hostName)_ssd1_boot; \
54 zpool create -o ashift=12 \
55 -O utf8only=on \
56 -R /mnt/$(hostName) $(zpool) /dev/disk/by-partlabel/$(hostName)_ssd1_zpool; \
57 zpool set autotrim=$(autotrim) $(zpool); \
58 zfs set \
59 acltype=off \
60 atime=off \
61 canmount=off \
62 compression=$(compression) \
63 dnodesize=auto \
64 relatime=on \
65 xattr=off \
66 mountpoint=none \
67 $(zpool); \
68 zfs create -o canmount=off -o mountpoint=none $(zpool)/reserved; \
69 zfs set refreservation=$(reservation) $(zpool)/reserved; \
70 zfs create -o canmount=on -o mountpoint=/ \
71 $(if $(cipher),-o encryption=$(cipher) \
72 -o keyformat=passphrase \
73 -o keylocation=prompt) \
74 $(zpool)/root; \
75 for p in nix home var; do \
76 zfs create $(zpool)/root/\$$p; \
77 done; \
78 zfs set acltype=posixacl xattr=sa $(zpool)/root/var; \
79 "'
80 #https://askubuntu.com/questions/970886/journalctl-says-failed-to-search-journal-acl-operation-not-supported
81
82 #sudo zfs set sync=disabled $(zpool)/root/var/tmp
83 #sudo zfs set copies=2 $(zpool)/root/home/files
84
85 copy-ssh:
86 host=$(TARGET); host=$${host#*@}; ssh-keygen -R $$host
87 ssh-copy-id $(TARGET)
88 #ssh -oForwardAgent=yes nixos@192.168.3.101 ssh-copy-id -i .ssh/id_ed25519.pub julm@192.168.3.1
89
90 install: NIX_STORE_DIR=/nix/store
91 install: targetRoot=/mnt/$(hostName)
92 install: targetStore=store=$(NIX_STORE_DIR)&remote-store=$(targetRoot)%3fstore=$(NIX_STORE_DIR)%26real=$(targetRoot)$(NIX_STORE_DIR)
93 install:
94 nix -L build --out-link ../$(hostName).root \
95 "../..#nixosConfigurations.$(hostName).config.system.build.toplevel"
96 ssh $(TARGET) sh -xec '" \
97 zpool list $(zpool) || zpool import $(zpool); \
98 test \$$(zfs get -H encryption -o value $(zpool)/root) = off || \
99 test \$$(zfs get -H keystatus -o value $(zpool)/root) = available || \
100 zfs load-key $(zpool)/root; \
101 mountpoint $(targetRoot) || \
102 mount -v -o zfsutil,X-mount.mkdir -t zfs $(zpool)/root $(targetRoot); \
103 mountpoint $(targetRoot)/boot1 || \
104 mount -v -o X-mount.mkdir /dev/disk/by-partlabel/$(hostName)_ssd1_boot $(targetRoot)/boot1; \
105 mountpoint $(targetRoot)/nix || \
106 mount -v -o zfsutil,X-mount.mkdir -t zfs $(zpool)/root/nix $(targetRoot)/nix; \
107 mountpoint $(targetRoot)/var || \
108 mount -v -o zfsutil,X-mount.mkdir -t zfs $(zpool)/root/var $(targetRoot)/var; \
109 findmnt \
110 "'
111 nix copy --to "ssh://$(TARGET)?$(targetStore)" ../courge.root
112 gpg -d credential.secret.gpg | \
113 ssh $(TARGET) sh -xec '" \
114 nix-env --store $(targetRoot) -p $(targetRoot)/nix/var/nix/profiles/system \
115 --set '$$(readlink -f ../$(hostName).root)'; \
116 mkdir -m 0755 -p $(targetRoot)/etc; \
117 touch $(targetRoot)/etc/NIXOS; \
118 install -D -o root -g root -m 400 /dev/stdin $(targetRoot)/var/lib/systemd/credential.secret; \
119 "'
120 ssh $(TARGET) nixos-enter --root $(targetRoot) -c '" \
121 set -eux; \
122 mount --rbind --mkdir / $(targetRoot); \
123 mount --make-rslave $(targetRoot); \
124 NIXOS_INSTALL_BOOTLOADER=1 $(shell realpath -e ../courge.root)/bin/switch-to-configuration boot; \
125 umount -R $(targetRoot) && rmdir $(targetRoot) \
126 "'