about summary refs log tree commit diff stats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xbootstrap/install/install.sh37
-rw-r--r--system/fileSystemLayouts/default.nix85
2 files changed, 115 insertions, 7 deletions
diff --git a/bootstrap/install/install.sh b/bootstrap/install/install.sh
index c4e8eefe..91caa186 100755
--- a/bootstrap/install/install.sh
+++ b/bootstrap/install/install.sh
@@ -83,6 +83,7 @@ bsc nix-store
 bsc persistent-storage
 bsc persistent-storage/nixos-config
 bsc persistent-storage/.snapshots
+bsc swap
 cd /
 umount -R /mnt
 mount -t tmpfs none /mnt
@@ -90,11 +91,47 @@ mount --mkdir "$DISK_EFI" /mnt/boot
 
 mne nix-store /mnt/nix
 mne persistent-storage /mnt/srv
+mne swap /mnt/swap
 
 mount --mkdir --options bind /mnt/srv/nixos-config /mnt/etc/nixos
 
 msg "Finished mounting and generating btrfs subvolumes"
 
+msg "Creating swapfile..."
+btrfs filesystem mkswapfile --size "$(free -m | awk '{if (NR==2) {printf "%sm\n", $2 + 500}}')" /mnt/swap/swapfile
+resume_offset=$(btrfs inspect-internal map-swapfile -r /mnt/swap/swapfile);
+swap_uuid=$(findmnt -no UUID -T /mnt/swap/swapfile);
+msg "Finished creating the swapfile";
+msg2 "UUID is: " "$swap_uuid";
+msg2 "Resume offset is: " "$resume_offset";
+
+msg "Checking for incompatibilities..."
+ssd_or_hdd=$(cat /sys/block/$disk/queue/rotational);
+case "$ssd_or_hdd" in
+    0)
+        msg "You seem to use a ssd."
+        trim_support=$(lsblk --bytes --json --discard | jq --arg name "$disk" '.blockdevices | .[] | select(.name == $name) | (.["disc-gran"] + .["disc-max"]) != 0');
+        case $trim_support in
+            "true")
+                msg2 "Yay, your ssd supports trim, go on and activate it";
+                ;;
+            "false")
+                msg2 "Nay, your ssd doesn't support trim, go on";
+                ;;
+           *)
+                warning "Your ssd doesn't seem to exists, if this bothers you please open an issue.'";
+                ;;
+        esac
+        ;;
+    1)
+        msg "You seem to use a hdd there is nothing you have to do";
+        ;;
+    *)
+        warning "There is no indicator, which shows, which drive your are using.\n This means, that you have to check yourself, which optimizations you should activate."
+        ;;
+esac
+
+
 readp "Do you want to continue with nixos-install? [N/y]: " result
 case $result in
     [Yy])
diff --git a/system/fileSystemLayouts/default.nix b/system/fileSystemLayouts/default.nix
index 215f01d0..f6d374d7 100644
--- a/system/fileSystemLayouts/default.nix
+++ b/system/fileSystemLayouts/default.nix
@@ -1,3 +1,4 @@
+# vim: ts=2
 {
   config,
   lib,
@@ -23,9 +24,65 @@ in {
       example = literalExpression "/dev/disk/by-uuid/5143-6136";
       description = lib.mdDoc "Path to the main disk";
     };
+    ssd = mkOption {
+      type = lib.types.bool;
+      example = literalExpression "true";
+      default = false;
+      description = lib.mdDoc "Enable ssd specific improvements?";
+    };
+    swap = {
+      uuid = mkOption {
+        type = lib.types.str;
+        example = literalExpression "d1d20ae7-3d8a-44da-86da-677dbbb10c89";
+        description = lib.mdDoc "The uuid of the swapfile";
+      };
+      resumeOffset = mkOption {
+        type = lib.types.int;
+        example = literalExpression "134324224";
+        description = lib.mdDoc "The resume offset of the swapfile";
+      };
+    };
   };
 
   config = mkIf cfg.enable {
+    systemd = mkIf cfg.ssd {
+      timers.fstrim = {
+        wantedBy = ["timers.target"];
+        wants = ["fstrim.service"];
+        unitConfig = {
+          Description = "Discard unused blocks once a week";
+          Documentation = "man:fstrim";
+          ConditionVirtualization = "!container";
+          ConditionPathExists = "!/etc/initrd-release";
+        };
+        timerConfig = {
+          OnCalendar = "weekly";
+          AccuracySec = "1h";
+          Persistent = "true";
+          RandomizedDelaySec = "6000";
+        };
+      };
+      services.fstrim = {
+        unitConfig = {
+          Description = "Discard unused blocks on filesystems from /etc/fstab";
+          Documentation = "man:fstrim(8)";
+          ConditionVirtualization = "!container";
+        };
+        serviceConfig = {
+          Type = "oneshot";
+          ExecStart = "/usr/bin/fstrim --listed-in /etc/fstab:/proc/self/mountinfo --verbose --quiet-unsupported";
+          PrivateDevices = "no";
+          PrivateNetwork = "yes";
+          PrivateUsers = "no";
+          ProtectKernelTunables = "yes";
+          ProtectKernelModules = "yes";
+          ProtectControlGroups = "yes";
+          MemoryDenyWriteExecute = "yes";
+          SystemCallFilter = "@default @file-system @basic-io @system-service";
+        };
+      };
+    };
+
     fileSystems = {
       "/" = {
         device = "none";
@@ -43,17 +100,31 @@ in {
         neededForBoot = true;
         options = ["subvol=persistent-storage"] ++ defaultMountOptions;
       };
+      "/swap" = {
+        device = cfg.mainDisk;
+        fsType = "btrfs";
+        neededForBoot = true;
+        options = ["subvol=swap"] ++ defaultMountOptions;
+      };
       "/boot" = {
         device = cfg.efiDisk;
         fsType = "vfat";
       };
-      #"${config.users.users.soispha.home}" = { # TODO this causes infinite recursion
-      #      "/home" = {
-      #        device = "none";
-      #        fsType = "tmpfs"; # Can be stored on normal drive or on tmpfs as well
-      #        options = ["defaults" "size=4G" "mode=755"];
-      #      };
     };
-    swapDevices = [];
+    swapDevices = [
+      {
+        device = "/swap/swapfile";
+        discardPolicy = "both"; # TODO this is the default in swapon, so it should be fine?
+        priority = 1; # should not be used, aside of hibernate
+      }
+    ];
+    zramSwap = {
+      enable = true;
+      priority = 10; # needs to be higher than harware-swap
+    };
+    boot.kernelParams = [
+      "resume=UUID=${cfg.swap.uuid}"
+      "resume_offset=${cfg.swap.resumeOffset}"
+    ];
   };
 }