diff options
Diffstat (limited to '')
| -rwxr-xr-x | modules/by-name/di/direnv/landrun-fish.sh | 30 | ||||
| -rw-r--r-- | modules/by-name/di/direnv/module.nix | 85 | ||||
| -rwxr-xr-x | modules/by-name/di/direnv/pre-cache.sh | 89 | ||||
| -rw-r--r-- | modules/by-name/di/disks/hibernate.nix | 7 | ||||
| -rw-r--r-- | modules/by-name/di/disks/module.nix | 85 |
5 files changed, 250 insertions, 46 deletions
diff --git a/modules/by-name/di/direnv/landrun-fish.sh b/modules/by-name/di/direnv/landrun-fish.sh new file mode 100755 index 00000000..09687afb --- /dev/null +++ b/modules/by-name/di/direnv/landrun-fish.sh @@ -0,0 +1,30 @@ +#! /usr/bin/env sh + +export NO_DIRENV_LOAD=true +landrun \ + --env PATH \ + --env USER \ + --env IN_NIX_SHELL \ + --env NIX_SHELL_LEVEL \ + --env NO_DIRENV_LOAD \ + --env SHLVL \ + --rox /nix/store \ + --ro /etc/ \ + --ro /proc/ \ + --rw /dev/ \ + --rw "/run/user/$(id --user)" \ + --ro "$HOME/.config" \ + --rw "$HOME/.cache" \ + --ro "$HOME/.local/share/nix/trusted-settings.json" \ + --rw "$HOME/.local/share/nix/repl-history" \ + --rw "$HOME/.local/share/task/" \ + --rw "$HOME/.local/share/nvim/" \ + --rw "$HOME/.local/share/cargo/" \ + --rw "$HOME/.local/share/ctpv/" \ + --rw "$HOME/.local/share/direnv" \ + --ro "$HOME/.config/fish/" \ + --connect-tcp 80 \ + --connect-tcp 443 \ + -- fish "$@" + +# vim: ft=sh diff --git a/modules/by-name/di/direnv/module.nix b/modules/by-name/di/direnv/module.nix index 7c81e671..ad47a2a7 100644 --- a/modules/by-name/di/direnv/module.nix +++ b/modules/by-name/di/direnv/module.nix @@ -10,21 +10,92 @@ { config, lib, + pkgs, + sources, ... }: let cfg = config.soispha.programs.direnv; + direnvCfg = config.home-manager.users.soispha.programs.direnv; + + inherit (sources) devshells nixpkgs; + + pre-cache = pkgs.runCommand "pre-cache.sh" {} '' + cat "${./pre-cache.sh}" > $out + + substituteInPlace $out \ + --replace-fail "%DEVSHELLS_REPO%" "${devshells}" \ + --replace-fail "%DEVSHELLS_REPO_REF%" "${devshells.sourceInfo.rev}.${builtins.readFile "${nixpkgs}/.git-revision"}" + ''; + + landrun-fish = pkgs.writeShellApplication { + name = "landrun-fish"; + text = builtins.readFile ./landrun-fish.sh; + inheritPath = true; + runtimeInputs = [ + pkgs.landrun + ]; + }; in { options.soispha.programs.direnv = { enable = lib.mkEnableOption "direnv"; }; - config.home-manager.users.soispha.programs.direnv = lib.mkIf cfg.enable { - enable = true; - nix-direnv.enable = true; - config = { - warn_timeout = 0; - # strict_env = true; - # disable_stdin = true; + config = lib.mkIf cfg.enable { + soispha.programs.fish.integrations.direnv = + # fish + '' + function __direnv_export_eval + ${lib.getExe direnvCfg.package} export fish | source; + end + + if test -z "$NO_DIRENV_LOAD" + function _direnv_hook + if test -f ".env" -o -f ".envrc" -o -f ".nenvrc" + # ${lib.getExe landrun-fish} --interactive --command='__direnv_export_eval' + __direnv_export_eval + end + end + + function __direnv_on_prompt --on-event fish_prompt; + _direnv_hook + end + + function _direnv_on_preexec --on-event fish_preexec; + _direnv_hook + end + end + ''; + + home-manager.users.soispha = { + programs.direnv = { + enable = true; + + package = pkgs.direnv-patched; + + nix-direnv = { + enable = true; + package = pkgs.nix-direnv.override {nix = config.nix.package;}; + }; + + # Handled by myself (and the script is overridden) + enableBashIntegration = true; + enableZshIntegration = true; + enableFishIntegration = false; + + config = { + warn_timeout = 0; + + # NOTE: Something variable is undeclared in the my default loaded stuff :/. <2026-08-18> + # strict_env = true; + + # disable_stdin = true; + }; + }; + xdg.configFile = { + "direnv/lib/hm-pre-cache.sh" = { + source = pre-cache; + }; + }; }; }; } diff --git a/modules/by-name/di/direnv/pre-cache.sh b/modules/by-name/di/direnv/pre-cache.sh new file mode 100755 index 00000000..706dae61 --- /dev/null +++ b/modules/by-name/di/direnv/pre-cache.sh @@ -0,0 +1,89 @@ +#! /usr/bin/env sh + +_pre_cache_log() { + printf "pre-cache: %s\n" "$1" +} +_pre_cache_preflight() { + set -eu + _nix_direnv_preflight + + REPO="%DEVSHELLS_REPO%" + system="x86_64-linux" + cache_dir="$XDG_CACHE_HOME/pre_cache" + [ -d "$cache_dir" ] || mkdir --parents "$cache_dir" + + repo_ref="%DEVSHELLS_REPO_REF%" + final_cache="$cache_dir/$repo_ref" + + if ! [ -d "$final_cache" ]; then + _pre_cache_log "Cache dir invalidated" + + # There is not yet a final cache directory, so the ref must have changed. + # Therefore we delete the whole cache directory, so we invalidate old cached shells. + set -u + rm --recursive "$cache_dir" + + mkdir --parents "$final_cache" + fi + + # TODO: Direnv requires `-u` to be unset, for it's stdlib :/. <2026-08-22> + set +u +} + +_pre_cache_load_shell() { + lang="$1" + lang_cache="$final_cache/$lang" + + if ! [ -f "$lang_cache" ]; then + tmp_profile="$final_cache/__tmp_profile" + profile="$final_cache/__profile" + + # It does not already exists, we need to evaluate it. + _nix print-dev-env --profile "$tmp_profile" "$REPO#$lang" >"$lang_cache" + + _nix_add_gcroot "$tmp_profile" "$profile" + # rm "$tmp_profile" "$tmp_profile"* || true + fi + + _pre_cache_log "Loaded dev-shell for $lang." + _nix_import_env "$lang_cache" +} + +_pre_cache_build_shells() { + installables="" + + for lang in "$@"; do + lang_cache="$final_cache/$lang" + + if ! [ -f "$lang_cache" ]; then + new_part="$REPO#devShells.$system.$lang" + + if [ -z "$installables" ]; then + installables="$new_part" + else + installables="$installables $new_part" + fi + fi + done + + if [ -n "$installables" ]; then + _pre_cache_log "Building '$installables'..." + + # We want shell-spliting + # shellcheck disable=SC2086 + _nix build --no-link $installables + fi +} + +pre_cache_load() { + _pre_cache_preflight + + # Build all not-yet build shells concurrently. That speeds up the initial load. + _pre_cache_build_shells "$@" + + for lan in "$@"; do + _pre_cache_load_shell "$lan" + done +} + +# vim: ft=sh diff --git a/modules/by-name/di/disks/hibernate.nix b/modules/by-name/di/disks/hibernate.nix index e983f9c7..d3f8fe8b 100644 --- a/modules/by-name/di/disks/hibernate.nix +++ b/modules/by-name/di/disks/hibernate.nix @@ -48,7 +48,8 @@ systemd-hibernate.serviceConfig.Environment = "SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1"; systemd-logind.serviceConfig.Environment = "SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1"; }; - sleep.extraConfig = '' - HibernateDelaySec=5m - ''; + + sleep.settings.Sleep = { + HibernateDelaySec = "10m"; + }; } diff --git a/modules/by-name/di/disks/module.nix b/modules/by-name/di/disks/module.nix index 3e9d4614..ed5c939a 100644 --- a/modules/by-name/di/disks/module.nix +++ b/modules/by-name/di/disks/module.nix @@ -14,10 +14,9 @@ modules, ... }: let - # FIXME: The iso redeploy requires a bigger efi partition <2024-05-12> cfg = config.soispha.disks; defaultMountOptions = [ - "compress=zstd:3" # This saves disk space, at a performance cost + "compress-force=zstd:15" # This saves disk space, at a performance cost "noatime" # should have some performance upsides, and I don't use it anyways "lazytime" # make time changes in memory ]; @@ -34,15 +33,10 @@ in { ssd = lib.mkEnableOption "ssd specific improvements, like trim"; swap = { - uuid = lib.mkOption { + ram_size = lib.mkOption { type = lib.types.str; - example = lib.literalExpression "d1d20ae7-3d8a-44da-86da-677dbbb10c89"; - description = "The uuid of the swapfile"; - }; - resumeOffset = lib.mkOption { - type = lib.types.str; - example = lib.literalExpression "134324224"; - description = "The resume offset of the swapfile"; + example = lib.literalExpression "16G"; + description = "The size of the ram (translates to the swapfile size)"; }; }; }; @@ -54,6 +48,12 @@ in { config = lib.mkIf cfg.enable { systemd = lib.recursiveUpdate (import ./hibernate.nix {inherit pkgs;}) (import ./fstrim.nix {inherit pkgs lib cfg;}); + services.btrfs.autoScrub = { + enable = true; + fileSystems = ["/srv" "/nix"]; + interval = "monthly"; + }; + disko.devices = { disk = { main = { @@ -61,21 +61,44 @@ in { content = { type = "gpt"; partitions = { + ESP = { + # 2GiB plus 512MiB for tails ISO and normal boot stuff + size = "2600M"; + + type = "EF00"; + content = { + type = "filesystem"; + format = "vfat"; + mountpoint = "/boot"; + mountOptions = ["umask=0077"]; + }; + }; + + nix = { + size = "30G"; + content = { + type = "luks"; + name = "nixos-store"; + extraOpenArgs = ["--allow-discards"]; + content = { + type = "btrfs"; + extraArgs = ["-f" "--label nixos-store"]; # Override existing partitions + mountpoint = "/nix"; + mountOptions = defaultMountOptions; + }; + }; + }; + root = { size = "100%"; - name = "root"; content = { type = "luks"; - name = "nixos"; + name = "nixos-root"; extraOpenArgs = ["--allow-discards"]; content = { type = "btrfs"; - extraArgs = ["-f" "--label nixos"]; # Override existing partitions + extraArgs = ["-f" "--label nixos-root"]; # Override existing partitions subvolumes = { - "nix" = { - mountpoint = "/nix"; - mountOptions = defaultMountOptions; - }; "persistent-storage" = { mountpoint = "/srv"; mountOptions = defaultMountOptions; @@ -90,21 +113,17 @@ in { "noatime" # should have some performance upsides, and I don't use it anyways "lazytime" # make time changes in memory ]; + swap = { + swapfile = { + priority = -1; # lower than zramSwap, just in case + size = cfg.swap.ram_size; + }; + }; }; }; }; }; }; - boot = { - type = "EF00"; - size = "512M"; - name = "boot"; - content = { - type = "filesystem"; - format = "vfat"; - mountpoint = "/boot"; - }; - }; }; }; }; @@ -130,6 +149,9 @@ in { }; }; fileSystems = { + "/nix" = { + neededForBoot = true; + }; "/srv" = { neededForBoot = true; }; @@ -137,23 +159,14 @@ in { neededForBoot = true; }; }; - swapDevices = [ - #{ - # device = "/swap/swapfile"; - # priority = 1; # lower than zramSwap, just in case - # # size = 2048; # TODO: can nixos create a btrfs swapfile correctly? - #} - ]; zramSwap = { enable = true; priority = 10; # needs to be higher than hardware-swap }; boot = { kernelParams = [ - "resume_offset=${cfg.swap.resumeOffset}" "zswap.enabled=0" # zswap and zram are not really compatible ]; - resumeDevice = "/dev/disk/by-uuid/${cfg.swap.uuid}"; }; }; } |
