]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/oignon/Makefile
nix: use local caches through SSH
[julm/julm-nix.git] / hosts / oignon / Makefile
1 #cwd := $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
2 machine := oignon
3 disk_ssd := /dev/disk/by-id/ata-Samsung_SSD_850_PRO_128GB_S1SMNSAFC36436X
4 zpool := $(machine)
5 cipher := aes-128-gcm
6 autotrim := on
7 reservation := 1G
8 # FIXME: use zstd when zfs stable gets it
9 compression := lz4
10
11 wipe:
12 sudo modprobe zfs
13 sudo zpool labelclear -f /dev/disk/by-partlabel/$(machine)_ssd_zpool || true
14 sudo $$(which sgdisk) --zap-all $(disk_ssd)
15
16 part: wipe
17 # https://wiki.archlinux.org/index.php/BIOS_boot_partition
18 sudo $$(which sgdisk) -a1 -n0:34:2047 -t0:EF02 -c0:"$(machine)_ssd_bios" $(disk_ssd)
19 sudo $$(which sgdisk) -n0:1M:+32M -t0:EF00 -c0:"$(machine)_ssd_efi" $(disk_ssd)
20 sudo $$(which sgdisk) -n0:0:+256M -t0:8300 -c0:"$(machine)_ssd_boot" $(disk_ssd)
21 sudo $$(which sgdisk) -n0:0:+4G -t0:8200 -c0:"$(machine)_ssd_swap" $(disk_ssd)
22 sudo $$(which sgdisk) -n0:0:0 -t0:BF01 -c0:"$(machine)_ssd_zpool" $(disk_ssd)
23 # https://wiki.archlinux.org/index.php/Partitioning#Tricking_old_BIOS_into_booting_from_GPT
24 printf '\200\0\0\0\0\0\0\0\0\0\0\0\001\0\0\0' | sudo dd of=$(disk_ssd) bs=1 seek=462
25 sudo $$(which sgdisk) --randomize-guids $(disk_ssd)
26 sudo $$(which sgdisk) --backup=$(machine)_ssd.sgdisk $(disk_ssd)
27
28 format: umount format-efi format-boot format-zpool
29 format-efi:
30 sudo blkid /dev/disk/by-partlabel/$(machine)_ssd_efi -t TYPE=vfat || \
31 sudo mkfs.vfat -F 16 -s 1 -n EFI /dev/disk/by-partlabel/$(machine)_ssd_efi
32 format-boot:
33 sudo mkdir -p /mnt/$(machine)
34 sudo blkid -t TYPE=ext2 /dev/disk/by-partlabel/$(machine)_ssd_boot; test $$? != 2 || \
35 sudo mkfs.ext2 /dev/disk/by-partlabel/$(machine)_ssd_boot
36 format-zpool:
37 sudo zpool list $(zpool) 2>/dev/null || \
38 sudo zpool create -o ashift=12 \
39 -O utf8only=yes \
40 $(if $(cipher),-O encryption=$(cipher) \
41 -O keyformat=passphrase \
42 -O keylocation=prompt) \
43 -R /mnt/$(machine) $(zpool) /dev/disk/by-partlabel/$(machine)_ssd_zpool
44 sudo zpool set \
45 autotrim=$(autotrim) \
46 $(zpool)
47 sudo zfs set \
48 acltype=off \
49 atime=off \
50 canmount=off \
51 compression=$(compression) \
52 dnodesize=auto \
53 relatime=on \
54 xattr=off \
55 mountpoint=/ \
56 $(zpool)
57 # https://nixos.wiki/wiki/NixOS_on_ZFS#Reservations
58 sudo zfs list $(zpool)/reserved 2>/dev/null || \
59 sudo zfs create -o canmount=off -o mountpoint=none $(zpool)/reserved
60 sudo zfs set refreservation=$(reservation) $(zpool)/reserved
61 # /
62 # mountpoint=legacy is required to let NixOS mount the ZFS filesystems.
63 sudo zfs list $(zpool)/root 2>/dev/null || \
64 sudo zfs create \
65 -o canmount=on \
66 -o mountpoint=legacy \
67 $(zpool)/root
68 # /*
69 for p in \
70 nix \
71 var \
72 ; do \
73 sudo zfs list $(zpool)/"$$p" 2>/dev/null || \
74 sudo zfs create \
75 -o canmount=on \
76 -o mountpoint=legacy \
77 $(zpool)/"$$p" ; \
78 done
79 #sudo zfs set sync=disabled $(zpool)/var/tmp
80 #sudo zfs set copies=2 $(zpool)/home/documents
81
82 mount: mount-zpool mount-boot mount-efi
83 mount-zpool:
84 # scan needed zpools
85 sudo zpool list $(zpool) || \
86 sudo zpool import -f $(zpool)
87 # load encryption key
88 sudo zfs get -H encryption $(zpool) | \
89 grep -q '^$(zpool)\s*encryption\s*off' || \
90 sudo zfs get -H keystatus $(zpool) | \
91 grep -q '^$(zpool)\s*keystatus\s*available' || \
92 sudo zfs load-key $(zpool)
93 # /
94 sudo mkdir -p /mnt/$(machine)
95 sudo mountpoint /mnt/$(machine) || \
96 sudo mount -v -t zfs $(zpool)/root /mnt/$(machine)
97 # /*
98 for p in \
99 nix \
100 var \
101 ; do \
102 sudo mkdir -p /mnt/$(machine)/"$$p"; \
103 sudo mountpoint /mnt/$(machine)/"$$p" || \
104 sudo mount -v -t zfs $(zpool)/"$$p" /mnt/$(machine)/"$$p" ; \
105 done
106 #sudo chmod 1777 /mnt/$(machine)/var/tmp
107 mount-boot:
108 sudo mkdir -p /mnt/$(machine)/boot
109 sudo mountpoint /mnt/$(machine)/boot || \
110 sudo mount -v /dev/disk/by-partlabel/$(machine)_ssd_boot /mnt/$(machine)/boot
111 #sudo mount -v -t zfs bpool/boot /mnt/$(machine)/boot
112 mount-efi: | mount-boot
113 sudo mkdir -p /mnt/$(machine)/boot/efi
114 sudo mountpoint /mnt/$(machine)/boot/efi || \
115 sudo mount -v /dev/disk/by-partlabel/$(machine)_ssd_efi /mnt/$(machine)/boot/efi
116
117 bootstrap: mount
118 # Workaround https://dev.gnupg.org/T3908
119 chmod o+rw $$GPG_TTY $$XAUTHORITY
120
121 sudo --preserve-env \
122 $$(which nixos-install) \
123 --root /mnt/$(machine) \
124 --flake '.#$(machine)' \
125 --no-root-passwd \
126 --no-channel-copy \
127 --option allow-import-from-derivation true \
128 --show-trace
129
130 # End workaround https://dev.gnupg.org/T3908
131 chmod o-rw $$GPG_TTY $$XAUTHORITY
132
133 umount:
134 for p in \
135 boot/efi \
136 boot \
137 nix \
138 var \
139 "" \
140 ; do \
141 ! sudo mountpoint /mnt/$(machine)/"$$p" || \
142 sudo umount -v /mnt/$(machine)/"$$p" ; \
143 done
144 ! sudo zpool list $(zpool) 2>/dev/null || \
145 zfs get -H encryption $(zpool) | \
146 grep -q '^$(zpool)\s*encryption\s*off' || \
147 zfs get -H keystatus $(zpool) | \
148 grep -q '^$(zpool)\s*keystatus\s*unavailable' || \
149 sudo zfs unload-key $(zpool)
150 #! sudo zpool list bpool 2>/dev/null || \
151 #sudo zpool export bpool
152 ! sudo zpool list $(zpool) 2>/dev/null || \
153 sudo zpool export $(zpool)
154
155 unlock:
156 pass machines/$(machine)/zfs/zpool | \
157 NIXOPS_DEPLOYMENT="$${NIXOPS_DEPLOYMENT:-$(LOSURDO_DEPLOYMENT)}" \
158 nixops ssh $(machine) -p 2222 'zfs load-key $(zpool) && pkill zfs'