From a5410593d66692dffca408a545f1d7bb7c009a74 Mon Sep 17 00:00:00 2001 From: Dmitriy Kholkin Date: Sat, 8 Oct 2022 04:32:18 +0300 Subject: [PATCH] using zfs on workstation --- flake.lock | 8 +-- flake.nix | 9 ++- install/install-zfs.sh | 12 ++-- machines/AMD-Workstation/default.nix | 3 +- .../hardware-configuration.nix | 63 ++++++++++--------- machines/Dell-Laptop/default.nix | 1 + modules/devices.nix | 1 + profiles/services.nix | 5 +- profiles/virtualisation.nix | 27 ++++++++ 9 files changed, 84 insertions(+), 45 deletions(-) diff --git a/flake.lock b/flake.lock index 6c79921..2cbc66f 100644 --- a/flake.lock +++ b/flake.lock @@ -544,11 +544,11 @@ }, "nixpkgs_3": { "locked": { - "lastModified": 1664780719, - "narHash": "sha256-Oxe6la5dSqRfJogjtY4sRzJjDDqvroJIVkcGEOT87MA=", - "owner": "nixos", + "lastModified": 1664407130, + "narHash": "sha256-m4vHHvI+Ybjpd3Rn0Zu851P0DfST3UkfMa7SWO7+/dY=", + "owner": "NixOS", "repo": "nixpkgs", - "rev": "fd54651f5ffb4a36e8463e0c327a78442b26cbe7", + "rev": "c0b69f571968269f35561cc09c17c710d938389d", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 6121fa4..2f02e13 100644 --- a/flake.nix +++ b/flake.nix @@ -103,7 +103,7 @@ channelsConfig = { allowUnfree = true; }; channels.unstable.input = nixpkgs; channels.unstable-zfs.input = nixpkgs; - channels.unstable-zfs.patches = [ ./patches/zen-kernels.patch ]; + channels.unstable-zfs.patches = [ ./patches/update-zfs.patch ]; hostDefaults.system = "x86_64-linux"; hostDefaults.channelName = "unstable"; @@ -115,7 +115,12 @@ specialArgs = { inherit inputs; }; }; in (genAttrs hostnames mkHost) // { - # AMD-Workstation.channelName = "unstable-zfs"; + AMD-Workstation = { + system = builtins.readFile (./machines + "/AMD-Workstation/system"); + modules = [ (import (./machines + "/AMD-Workstation")) { device = "AMD-Workstation"; } ]; + specialArgs = { inherit inputs; }; + channelName = "unstable-zfs"; + }; }; outputsBuilder = channels: let diff --git a/install/install-zfs.sh b/install/install-zfs.sh index 7a4d2c8..9c93e38 100755 --- a/install/install-zfs.sh +++ b/install/install-zfs.sh @@ -4,11 +4,11 @@ set -e CONFIG_FOLDER="$(dirname "$(pwd)")" -DEVICE_NAME=Testing-VM -MAX_JOBS=4 -SWAP_SIZE=16GiB +DEVICE_NAME=AMD-Workstation +MAX_JOBS=12 +SWAP_SIZE=48GiB USE_ECNRYPTION=false -ZFS_ARC_MAX=1073741824 +ZFS_ARC_MAX=12884901888 # ZFS_ARC_MAX=8589934592 # 8GiB # ZFS_ARC_MAX=4294967296 # Max ARC cache size. default = 4GiB ZFS_ASHIFT=12 # recommended=12 which 1<<12 (4096) @@ -186,6 +186,8 @@ zfs create -o canmount=on -o mountpoint=/home rpool/user/home zfs create -o canmount=off -o mountpoint=/var rpool/nixos/var zfs create -o canmount=on rpool/nixos/var/lib zfs create -o canmount=on rpool/nixos/var/log +zfs create -o canmount=noauto -o atime=off rpool/nixos/lxd +zfs create -o canmount=on -o mountpoint=/var/lib/docker -o atime=off rpool/nixos/docker zfs create -o canmount=on -o mountpoint=/media/bittorrent -o atime=off -o recordsize=256K rpool/nixos/bittorrent zfs create -o canmount=on -o mountpoint=/media/libvirt -o atime=off -o recordsize=64K rpool/nixos/libvirt @@ -257,4 +259,4 @@ fi pprint "Copy config to destination system" mkdir -p /mnt/home/alukard/nixos-config -cp -aT $CONFIG_FOLDER /mnt/home/alukard/nixos-config \ No newline at end of file +cp -aT $CONFIG_FOLDER /mnt/home/alukard/nixos-config diff --git a/machines/AMD-Workstation/default.nix b/machines/AMD-Workstation/default.nix index 5c368fa..6e5b170 100644 --- a/machines/AMD-Workstation/default.nix +++ b/machines/AMD-Workstation/default.nix @@ -19,7 +19,8 @@ vendor = "amd"; }; bigScreen = true; - ram = 16; + ram = 48; + fileSystem = "zfs"; }; deviceSpecific.isHost = true; deviceSpecific.isShared = false; diff --git a/machines/AMD-Workstation/hardware-configuration.nix b/machines/AMD-Workstation/hardware-configuration.nix index 6b3eb56..2686a86 100644 --- a/machines/AMD-Workstation/hardware-configuration.nix +++ b/machines/AMD-Workstation/hardware-configuration.nix @@ -14,47 +14,38 @@ boot.extraModulePackages = [ ]; fileSystems."/" = - { device = "/dev/disk/by-partuuid/07fbbbc3-169c-463c-bd53-28dcedb8634d"; - fsType = "btrfs"; - options = [ "subvol=nixos" "compress-force=zstd" "noatime" "autodefrag" "ssd" ]; + { device = "rpool/nixos/root"; + fsType = "zfs"; options = [ "zfsutil" "X-mount.mkdir" ]; }; fileSystems."/nix" = - { device = "/dev/disk/by-partuuid/07fbbbc3-169c-463c-bd53-28dcedb8634d"; - fsType = "btrfs"; - options = [ "subvol=nix" "compress-force=zstd" "noatime" "autodefrag" "ssd" ]; + { device = "rpool/nixos/nix"; + fsType = "zfs"; options = [ "zfsutil" "X-mount.mkdir" ]; }; fileSystems."/home" = - { device = "/dev/disk/by-partuuid/07fbbbc3-169c-463c-bd53-28dcedb8634d"; - fsType = "btrfs"; - options = [ "subvol=home" "compress-force=zstd" "noatime" "autodefrag" "ssd" ]; + { device = "rpool/user/home"; + fsType = "zfs"; options = [ "zfsutil" "X-mount.mkdir" ]; }; - fileSystems."/var" = - { device = "/dev/disk/by-partuuid/07fbbbc3-169c-463c-bd53-28dcedb8634d"; - fsType = "btrfs"; - options = [ "subvol=var" "compress-force=zstd" "noatime" "autodefrag" "ssd" ]; + fileSystems."/var/lib" = + { device = "rpool/nixos/var/lib"; + fsType = "zfs"; options = [ "zfsutil" "X-mount.mkdir" ]; + }; + + fileSystems."/var/log" = + { device = "rpool/nixos/var/log"; + fsType = "zfs"; options = [ "zfsutil" "X-mount.mkdir" ]; }; fileSystems."/media/bittorrent" = - { device = "/dev/disk/by-partuuid/07fbbbc3-169c-463c-bd53-28dcedb8634d"; - fsType = "btrfs"; - options = [ - "subvol=bittorrent" "nodatacow" "ssd" - "uid=${toString config.users.users.alukard.uid}" - "gid=${toString config.users.groups.users.gid}" - ]; + { device = "rpool/nixos/bittorrent"; + fsType = "zfs"; options = [ "zfsutil" "X-mount.mkdir" ]; }; fileSystems."/media/libvirt" = - { device = "/dev/disk/by-partuuid/07fbbbc3-169c-463c-bd53-28dcedb8634d"; - fsType = "btrfs"; - options = [ - "subvol=libvirt" "nodatacow" "ssd" - "uid=${toString config.users.users.alukard.uid}" - "gid=${toString config.users.groups.users.gid}" - ]; + { device = "rpool/nixos/libvirt"; + fsType = "zfs"; options = [ "zfsutil" "X-mount.mkdir" ]; }; fileSystems."/boot" = @@ -64,14 +55,24 @@ swapDevices = [ { - device = "/dev/disk/by-partuuid/94696da5-f478-485d-8d92-c6f3093d8010"; + device = "/dev/disk/by-partuuid/7ffa34d9-862b-42ff-a649-da54f7b8fbf0"; randomEncryption.enable = true; } ]; + # Enables DHCP on each ethernet and wireless interface. In case of scripted networking + # (the default) this is the recommended approach. When using systemd-networkd it's + # still possible to use this option, but it's recommended to use it in conjunction + # with explicit per-interface declarations with `networking.interfaces..useDHCP`. + networking.useDHCP = lib.mkDefault true; + # networking.interfaces.enp9s0.useDHCP = lib.mkDefault true; + + nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux"; + hardware.cpu.amd.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware; # high-resolution display hardware.video.hidpi.enable = lib.mkDefault true; - networking.hostId = "0a9e92cd"; - boot.initrd.supportedFilesystems = [ "btrfs" ]; - boot.supportedFilesystems = [ "btrfs" ]; + networking.hostId = "a32bd2dc"; + boot.zfs.devNodes = "/dev/disk/by-partuuid/4c6b8cfb-9643-4ff7-961e-89b097328e0e"; + boot.supportedFilesystems = [ "zfs" ]; + boot.kernelParams = [ "zfs.zfs_arc_max=12884901888" "nohibernate" ]; } diff --git a/machines/Dell-Laptop/default.nix b/machines/Dell-Laptop/default.nix index 320cd41..f999307 100644 --- a/machines/Dell-Laptop/default.nix +++ b/machines/Dell-Laptop/default.nix @@ -20,6 +20,7 @@ }; bigScreen = false; ram = 16; + fileSystem = "btrfs"; }; deviceSpecific.isHost = false; deviceSpecific.isShared = false; diff --git a/modules/devices.nix b/modules/devices.nix index 93b9c95..99efc65 100644 --- a/modules/devices.nix +++ b/modules/devices.nix @@ -19,6 +19,7 @@ with types; { gpu = { vendor = mkOption { type = enum [ "amd" "nvidia" "intel" "vm" "other" ]; }; }; + fileSystem = mkOption { type = enum [ "btrfs" "zfs" "other" ]; default = "other"; }; ram = mkOption { type = int; }; legacy = mkOption { type = bool; default = false; }; bigScreen = mkOption { diff --git a/profiles/services.nix b/profiles/services.nix index b23c1a7..9731fc5 100644 --- a/profiles/services.nix +++ b/profiles/services.nix @@ -7,7 +7,7 @@ with config.deviceSpecific; { hardware.bluetooth.enable = !isServer; services.blueman.enable = !isServer; - services.btrbk.instances = { + services.btrbk.instances = lib.mkIf (devInfo.fileSystem == "btrfs") { home = { settings = { snapshot_preserve_min = "2d"; @@ -35,9 +35,10 @@ with config.deviceSpecific; { }; services.fstrim = { - enable = isSSD; + enable = isSSD && devInfo.fileSystem != "zfs"; interval = "weekly"; }; + services.zfs.trim.enable = isSSD && devInfo.fileSystem == "zfs"; services.gvfs.enable = !isServer; diff --git a/profiles/virtualisation.nix b/profiles/virtualisation.nix index 2117c9a..c02936b 100644 --- a/profiles/virtualisation.nix +++ b/profiles/virtualisation.nix @@ -3,6 +3,12 @@ with config.deviceSpecific; { config = lib.mkIf enableVirtualisation { virtualisation.docker = { enable = true; + storageDriver = if (devInfo.fileSystem == "zfs") then + "zfs" + else if (devInfo.fileSystem == "btrfs") then + "btrfs" + else + "overlay2"; }; virtualisation.oci-containers.backend = "docker"; @@ -17,6 +23,27 @@ with config.deviceSpecific; { onShutdown = "shutdown"; }; + virtualisation.lxd = { + enable = true; + zfsSupport = (devInfo.fileSystem == "zfs"); + recommendedSysctlSettings = true; + }; + virtualisation.lxc = { + enable = true; + lxcfs.enable = true; + systemConfig = '' + lxc.lxcpath = /var/lib/lxd/containers + ${if devInfo.fileSystem == "zfs" then '' + lxc.bdev.zfs.root = rpool/lxd + '' else ""} + ''; + defaultConfig = '' + lxc.idmap = u 0 100000 65535 + lxc.idmap = g 0 100000 65535 + lxc.include = ${pkgs.lxcfs}/share/lxc/config/common.conf.d/00-lxcfs.conf + ''; + }; + virtualisation.spiceUSBRedirection.enable = true; networking.nat = {