diff options
Diffstat (limited to 'system-profiles')
| -rw-r--r-- | system-profiles/nfsroot.nix | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/system-profiles/nfsroot.nix b/system-profiles/nfsroot.nix new file mode 100644 index 00000000..92cf98de --- /dev/null +++ b/system-profiles/nfsroot.nix | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | { config, pkgs, lib, flakeInputs, ... }: | ||
| 2 | |||
| 3 | with lib; | ||
| 4 | |||
| 5 | let | ||
| 6 | cfg = config.nfsroot; | ||
| 7 | in { | ||
| 8 | options = { | ||
| 9 | nfsroot = { | ||
| 10 | storeDevice = mkOption { | ||
| 11 | type = types.str; | ||
| 12 | default = "nfsroot:nix-store"; | ||
| 13 | }; | ||
| 14 | |||
| 15 | registrationUrl = mkOption { | ||
| 16 | type = types.str; | ||
| 17 | default = "http://nfsroot/nix-registration"; | ||
| 18 | }; | ||
| 19 | }; | ||
| 20 | |||
| 21 | system.build = { | ||
| 22 | storeContents = mkOption {}; | ||
| 23 | }; | ||
| 24 | }; | ||
| 25 | |||
| 26 | config = { | ||
| 27 | # Don't build the GRUB menu builder script, since we don't need it | ||
| 28 | # here and it causes a cyclic dependency. | ||
| 29 | boot.loader.grub.enable = false; | ||
| 30 | |||
| 31 | # !!! Hack - attributes expected by other modules. | ||
| 32 | environment.systemPackages = [ pkgs.grub2_efi ] | ||
| 33 | ++ (if pkgs.stdenv.hostPlatform.system == "aarch64-linux" | ||
| 34 | then [] | ||
| 35 | else [ pkgs.grub2 pkgs.syslinux ]); | ||
| 36 | |||
| 37 | fileSystems."/" = mkImageMediaOverride | ||
| 38 | { fsType = "tmpfs"; | ||
| 39 | options = [ "mode=0755" ]; | ||
| 40 | }; | ||
| 41 | |||
| 42 | # In stage 1, mount a tmpfs on top of /nix/store (the squashfs | ||
| 43 | # image) to make this a live CD. | ||
| 44 | fileSystems."/nix/.ro-store" = mkImageMediaOverride | ||
| 45 | { fsType = "nfs4"; | ||
| 46 | device = cfg.storeDevice; | ||
| 47 | options = [ "ro" ]; | ||
| 48 | neededForBoot = true; | ||
| 49 | }; | ||
| 50 | |||
| 51 | fileSystems."/nix/.rw-store" = mkImageMediaOverride | ||
| 52 | { fsType = "tmpfs"; | ||
| 53 | options = [ "mode=0755" ]; | ||
| 54 | neededForBoot = true; | ||
| 55 | }; | ||
| 56 | |||
| 57 | fileSystems."/nix/store" = mkImageMediaOverride | ||
| 58 | { fsType = "overlay"; | ||
| 59 | device = "overlay"; | ||
| 60 | options = [ | ||
| 61 | "lowerdir=/nix/.ro-store" | ||
| 62 | "upperdir=/nix/.rw-store/store" | ||
| 63 | "workdir=/nix/.rw-store/work" | ||
| 64 | ]; | ||
| 65 | |||
| 66 | depends = [ | ||
| 67 | "/nix/.ro-store" | ||
| 68 | "/nix/.rw-store/store" | ||
| 69 | "/nix/.rw-store/work" | ||
| 70 | ]; | ||
| 71 | }; | ||
| 72 | |||
| 73 | nix.extraOptions = '' | ||
| 74 | use-sqlite-wal = false | ||
| 75 | ''; | ||
| 76 | |||
| 77 | boot.initrd.availableKernelModules = [ "nfs" "nfsv4" "overlay" ]; | ||
| 78 | boot.initrd.supportedFilesystems = [ "nfs" "nfsv4" "overlay" ]; | ||
| 79 | services.rpcbind.enable = mkImageMediaOverride false; | ||
| 80 | |||
| 81 | boot.initrd.network.enable = true; | ||
| 82 | boot.initrd.network.flushBeforeStage2 = false; # otherwise nfs doesn't work | ||
| 83 | boot.initrd.postMountCommands = '' | ||
| 84 | mkdir -p /mnt-root/etc/ | ||
| 85 | cp /etc/resolv.conf /mnt-root/etc/resolv.conf | ||
| 86 | ''; | ||
| 87 | networking.useDHCP = true; | ||
| 88 | networking.resolvconf.enable = false; | ||
| 89 | |||
| 90 | |||
| 91 | system.build.storeContents = [config.system.build.toplevel]; | ||
| 92 | |||
| 93 | system.build.netbootIpxeScript = pkgs.writeTextDir "netboot.ipxe" '' | ||
| 94 | #!ipxe | ||
| 95 | # Use the cmdline variable to allow the user to specify custom kernel params | ||
| 96 | # when chainloading this script from other iPXE scripts like netboot.xyz | ||
| 97 | kernel ${pkgs.stdenv.hostPlatform.linux-kernel.target} init=${config.system.build.toplevel}/init initrd=initrd ${toString config.boot.kernelParams} ''${cmdline} | ||
| 98 | initrd initrd | ||
| 99 | boot | ||
| 100 | ''; | ||
| 101 | |||
| 102 | boot.postBootCommands = | ||
| 103 | '' | ||
| 104 | # After booting, register the contents of the Nix store on NFS | ||
| 105 | # in the Nix database in the tmpfs. | ||
| 106 | ${pkgs.curl}/bin/curl ${escapeShellArg cfg.registrationUrl} | ${config.nix.package.out}/bin/nix-store --load-db | ||
| 107 | |||
| 108 | # nixos-rebuild also requires a "system" profile and an | ||
| 109 | # /etc/NIXOS tag. | ||
| 110 | touch /etc/NIXOS | ||
| 111 | ${config.nix.package}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system | ||
| 112 | ''; | ||
| 113 | }; | ||
| 114 | } | ||
