server: add swap

This commit is contained in:
chayleaf 2023-10-17 23:12:08 +07:00
parent 18d471c2ec
commit 04f3546f6e
3 changed files with 42 additions and 24 deletions

View file

@ -4,19 +4,22 @@
# then decompress it back in a derivation (shouldn't there be a better way...) # then decompress it back in a derivation (shouldn't there be a better way...)
copyToStore = pkgs: name: path: copyToStore = pkgs: name: path:
let let
archive = exec [ archive = exec [ "${pkgs.bash}/bin/bash" "-c" ''
"/bin/sh" "-c" cd /etc/nixos/private
"echo '\"' && (cd /etc/nixos/private && tar -I ${pkgs.zstd}/bin/zstd -c -- ${pkgs.lib.escapeShellArg path} 2>/dev/null | base64 -w0) && echo '\"'" echo '"'"$(
]; ${pkgs.gnutar}/bin/tar -I ${pkgs.zstd}/bin/zstd --exclude-vcs \
in "${pkgs.stdenvNoCC.mkDerivation { --transform='s#'${pkgs.lib.escapeShellArg path}'#!#' \
-c -- ${pkgs.lib.escapeShellArg path} | base64 -w0
)"'"'
'' ];
in derivation {
inherit name; inherit name;
unpackPhase = "true"; inherit (pkgs) system;
buildPhase = "true"; builder = "${pkgs.bash}/bin/bash";
installPhase = '' args = [ "-c" ''
mkdir -p $out echo '${archive}' | ${pkgs.coreutils}/bin/base64 -d |
cd $out ${pkgs.gnutar}/bin/tar -P --transform="s#!#$out#" -I ${pkgs.zstd}/bin/zstd -x
echo "${archive}" | base64 -d | tar -I ${pkgs.zstd}/bin/zstd -x '' ];
''; };
}}/${toString path}";
}; };
} }

View file

@ -326,11 +326,11 @@
maxSilent = 60 * 60 * 10; maxSilent = 60 * 60 * 10;
}; };
}; };
in { in builtins.mapAttrs (_: addMeta) {
server.${config.server.system} = addMeta nixosConfigurations.server.config.system.build.toplevel; server.${config.server.system} = nixosConfigurations.server.config.system.build.toplevel;
workstation.${config.nixmsi.system} = addMeta nixosConfigurations.nixmsi.config.system.build.toplevel; workstation.${config.nixmsi.system} = nixosConfigurations.nixmsi.config.system.build.toplevel;
router.${config.router-emmc.system} = addMeta nixosConfigurations.router-emmc-cross.config.system.build.toplevel; router.${config.router-emmc.system} = nixosConfigurations.router-emmc-cross.config.system.build.toplevel;
workstation-home.${config.nixmsi.system} = addMeta homeConfigurations."user@nixmsi".activation-script; workstation-home.${config.nixmsi.system} = homeConfigurations."user@nixmsi".activation-script;
}; };
}; };
} }

View file

@ -59,18 +59,33 @@ in
}; };
}; };
fileSystems = { fileSystems = let
"/" = { device = "none"; fsType = "tmpfs"; neededForBoot = true; device = rootPart;
fsType = "btrfs";
neededForBoot = true;
compress = "compress=zstd";
discard = "discard=async";
in {
"/" = { device = "none"; fsType = "tmpfs"; inherit neededForBoot;
options = [ "defaults" "size=2G" "mode=755" ]; }; options = [ "defaults" "size=2G" "mode=755" ]; };
# TODO: switch to bcachefs? # TODO: switch to bcachefs?
# I wanna do it some day, but maybe starting with the next disk I get for this server # I wanna do it some day, but maybe starting with the next disk I get for this server
"/persist" = "/persist" =
{ device = rootPart; fsType = "btrfs"; neededForBoot = true; { inherit device fsType neededForBoot;
options = [ "subvol=@" "compress=zstd" ]; }; options = [ discard compress "subvol=@" ]; };
"/boot" = "/swap" = { inherit device fsType neededForBoot;
{ device = bootPart; fsType = "vfat"; neededForBoot = true; }; options = [ discard "subvol=@swap" "noatime" ]; };
"/boot" = { device = bootPart; fsType = "vfat"; inherit neededForBoot; };
}; };
swapDevices = [ { device = "/swap/swapfile"; } ];
boot.kernelParams = [
"resume=/@swap/swapfile"
# resume_offset = $(btrfs inspect-internal map-swapfile -r path/to/swapfile)
"resume_offset=26001976"
];
impermanence = { impermanence = {
enable = true; enable = true;
path = /persist; path = /persist;