1 #cwd := $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
 
   3 disk_ssd1   := /dev/disk/by-id/nvme-INTEL_SSDPEKKF256G8L_BTHP91220R5E256B
 
   9 TARGET      ?= root@192.168.3.101
 
  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
 
  21 ssh/host.key ssh/host.key.pub:
 
  23         ssh-keygen -t ed25519 -f $@
 
  24 ssh/host.key.gpg: ssh/host.key
 
  25         gpg --encrypt $(shell printf -- ' -r %s' $$(cat .gpg-id)) $<
 
  27 ssh/host.key.cred: ssh/host.key.gpg
 
  28         ../gpg2cred-local.sh $< host.key
 
  31         ssh $(TARGET) sh -xec '" \
 
  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); \
 
  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); \
 
  53           mkfs.vfat -F 32 -s 1 -n EFI /dev/disk/by-partlabel/$(hostName)_ssd1_boot; \
 
  54           zpool create -o ashift=12 \
 
  56             -R /mnt/$(hostName) $(zpool) /dev/disk/by-partlabel/$(hostName)_ssd1_zpool; \
 
  57           zpool set autotrim=$(autotrim) $(zpool); \
 
  62             compression=$(compression) \
 
  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) \
 
  75           for p in nix home var; do \
 
  76             zfs create $(zpool)/root/\$$p; \
 
  78           zfs set acltype=posixacl xattr=sa $(zpool)/root/var; \
 
  80         #https://askubuntu.com/questions/970886/journalctl-says-failed-to-search-journal-acl-operation-not-supported
 
  82           #sudo zfs set sync=disabled $(zpool)/root/var/tmp
 
  83           #sudo zfs set copies=2 $(zpool)/root/home/files
 
  86         host=$(TARGET); host=$${host#*@}; ssh-keygen -R $$host
 
  88         #ssh -oForwardAgent=yes nixos@192.168.3.101 ssh-copy-id -i .ssh/id_ed25519.pub julm@192.168.3.1
 
  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)
 
  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; \
 
 111         nix copy --to "ssh://$(TARGET)?$(targetStore)" ../$(hostName).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; \
 
 120         ssh $(TARGET) nixos-enter --root $(targetRoot) -c '" \
 
 122           mount --rbind --mkdir / $(targetRoot); \
 
 123           mount --make-rslave $(targetRoot); \
 
 124           NIXOS_INSTALL_BOOTLOADER=1 '$$(realpath -e ../$(hostName).root)'/bin/switch-to-configuration boot; \
 
 125           umount -R $(targetRoot) && rmdir $(targetRoot) \