From 1255d382dc1e62c833d757b8e49cc2548f66ad22 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Wed, 3 Sep 2025 09:34:06 -0700 Subject: [PATCH] Adding zfsrent --- qemu/boot.sh | 2 +- zfsrent/configuration.nix | 144 ++++++++++++++++++++++++++++++++++++++ zfsrent/disko-gpt.nix | 127 +++++++++++++++++++++++++++++++++ 3 files changed, 272 insertions(+), 1 deletion(-) create mode 100644 zfsrent/configuration.nix create mode 100644 zfsrent/disko-gpt.nix diff --git a/qemu/boot.sh b/qemu/boot.sh index eed4c40..942f7ec 100755 --- a/qemu/boot.sh +++ b/qemu/boot.sh @@ -3,7 +3,7 @@ qemu-system-x86_64 \ -m 4G \ -smp 4 \ -cpu host \ - -drive file=/home/hunner/Downloads/latest-nixos-minimal-x86_64-linux.iso,media=cdrom \ + -drive file=/home/hunner/Downloads/nixos-minimal-24.11.716389.c570c1f53044-x86_64-linux.iso,media=cdrom \ -drive file=disk1.qcow2,format=qcow2,if=virtio \ -drive file=disk2.qcow2,format=qcow2,if=virtio \ -boot menu=on,splash-time=5000 \ diff --git a/zfsrent/configuration.nix b/zfsrent/configuration.nix new file mode 100644 index 0000000..db2d023 --- /dev/null +++ b/zfsrent/configuration.nix @@ -0,0 +1,144 @@ +{ config, pkgs, lib, ... }: + +#zpool import -f rpool +#mount -t zfs rpool/local/root /mnt +#mkdir -p /mnt/{boot,nix,home,persist,var/lib,var/log} +#mount /dev/vda2 /mnt/boot +#mount -t zfs rpool/local/nix /mnt/nix +#mount -t zfs rpool/safe/home /mnt/home +#mount -t zfs rpool/safe/persist /mnt/persist +#mount -t zfs rpool/local/var/lib /mnt/var/lib +#mount -t zfs rpool/local/var/log /mnt/var/log +let + impermanence = builtins.fetchTarball "https://github.com/nix-community/impermanence/archive/master.tar.gz"; +in +{ + imports = + [ + ./hardware-configuration.nix + "${impermanence}/nixos.nix" + ]; + + # Enable ZFS support + boot.supportedFilesystems = [ "zfs" ]; + boot.zfs.requestEncryptionCredentials = false; + + fileSystems = { + "/persist" = { + device = "rpool/safe/persist"; + fsType = "zfs"; + neededForBoot = true; # Only /persist needs to be marked as needed for boot + }; + }; + + # Import the existing ZFS pool from the second disk without formatting it + boot.zfs.extraPools = [ "tank" ]; + boot.zfs.devNodes = "/dev/disk/by-path"; # This is neede for ZFS to find the pool at boot + + # Use GRUB with BIOS booting, whether MBR or GPT + boot.loader.grub.enable = true; + boot.loader.grub.device = "/dev/vda"; + boot.loader.grub.zfsSupport = true; + boot.loader.grub.efiSupport = false; + + # Impermanence configuration + # Set up impermanence - root filesystem will be reset on each boot + boot.initrd.postResumeCommands = lib.mkAfter '' + zfs rollback -r rpool/local/root@blank + ''; + # /var/log and /var/lib ar persisted through zfs datasets, but not backed up. + # Anything stored in /persist should get backed up. + environment.persistence."/persist" = { + hideMounts = true; + directories = [ + "/root" + "/etc/nixos" + "/etc/ssh" + ]; + files = [ + "/etc/machine-id" + ]; + }; + # Files are not copied to /persist during install, so need to do so manually + #rsync -azPH /mnt/root/ /mnt/persist/root + #rsync -azPH /mnt/etc/nixos/ /mnt/persist/etc/nixos + #rsync -azPH /mnt/etc/ssh/ /mnt/persist/etc/ssh + #cp /mnt/etc/machine-id /mnt/persist/etc/machine-id + + # Swap configuration + swapDevices = [ { + device = "/dev/vda3"; + randomEncryption.enable = true; + } ]; + + # Basic system configuration + networking.hostId = "5472a981"; # Required for ZFS + networking.hostName = "cryochamber"; + + # Enable networking + networking.networkmanager.enable = true; + + # Don't lecture after reboot + security.sudo.extraConfig = '' + Defaults lecture = never + ''; + + # Set your time zone + time.timeZone = "UTC"; + + # Docker configuration + virtualisation.docker = { + enable = true; + extraOptions = "--storage-driver=overlay2"; + }; + + # System packages + environment.systemPackages = with pkgs; [ + git + vim + wget + curl + htop + zfs + zsh + tmux + docker-compose + ]; + + # Define a user account + users.users.hunner = { + isNormalUser = true; + extraGroups = [ "wheel" "docker" "networkmanager" ]; + hashedPassword = "$y$j9T$hLqdzlz7dbJZgUnKs.eo3/$25s/2X18vGtDKj53qD1sn/.Omp/6CBJWbn7d9KAiOK7"; + shell = pkgs.zsh; + packages = with pkgs; [ + fzf + neovim + devenv + pass + jq + yq + yt-dlp + bat + ripgrep + fd + shellcheck + tldr + unzip + lsof + ]; + }; + + programs.direnv = { + enable = true; + nix-direnv.enable = true; + }; + programs.zsh.enable = true; + services.openssh.enable = true; + services.openssh.settings.PermitRootLogin = "prohibit-password"; + + nix.settings.experimental-features = [ "nix-command" "flakes" ]; + nixpkgs.config.allowUnfree = true; + + system.stateVersion = "24.11"; +} diff --git a/zfsrent/disko-gpt.nix b/zfsrent/disko-gpt.nix new file mode 100644 index 0000000..b827da7 --- /dev/null +++ b/zfsrent/disko-gpt.nix @@ -0,0 +1,127 @@ +{ + disko.devices = { + disk = { + vda = { + type = "disk"; + device = "/dev/vda"; + content = { + type = "gpt"; + partitions = { + boot = { + size = "1M"; + type = "EF02"; + }; + ESP = { + name = "ESP"; + size = "512M"; + type = "EF00"; + content = { + type = "filesystem"; + format = "vfat"; + mountpoint = "/boot"; + mountOptions = [ "umask=0077" ]; + }; + }; + swap = { + size = "8G"; + type = "82"; + content = { + type = "swap"; + randomEncryption = true; + }; + }; + zfs = { + size = "100%"; + type = "83"; + content = { + type = "zfs"; + pool = "rpool"; + }; + }; + }; + }; + }; + }; + zpool = { + rpool = { + type = "zpool"; + rootFsOptions = { + acltype = "posixacl"; + compression = "lz4"; + dnodesize = "auto"; + normalization = "formD"; + relatime = "on"; + xattr = "sa"; + }; + options = { + ashift = "12"; + autotrim = "on"; + }; + datasets = { + # Non-filesystem datasets + "local" = { + type = "zfs_fs"; + options = { + mountpoint = "none"; + canmount = "off"; + }; + }; + "safe" = { + type = "zfs_fs"; + options = { + mountpoint = "none"; + canmount = "off"; + }; + }; + "local/var" = { + type = "zfs_fs"; + options = { + mountpoint = "none"; + canmount = "off"; + }; + }; + + # Local datasets + "local/root" = { + type = "zfs_fs"; + options = { + mountpoint = "legacy"; + canmount = "noauto"; + }; + mountpoint = "/"; + postCreateHook = '' + zfs snapshot rpool/local/root@blank + ''; + }; + "local/nix" = { + type = "zfs_fs"; + options.mountpoint = "legacy"; + mountpoint = "/nix"; + }; + "local/var/lib" = { + type = "zfs_fs"; + options.mountpoint = "legacy"; + mountpoint = "/var/lib"; + }; + "local/var/log" = { + type = "zfs_fs"; + options.mountpoint = "legacy"; + mountpoint = "/var/log"; + }; + + # Safe datasets + "safe/persist" = { + type = "zfs_fs"; + options.mountpoint = "legacy"; + mountpoint = "/persist"; + }; + "safe/home" = { + type = "zfs_fs"; + options.mountpoint = "legacy"; + mountpoint = "/home"; + }; + }; + }; + }; + }; +}