refactor(nixos): use new persistDirs option and remove environment.persistence where possible

This commit is contained in:
Mohammad Rafiq 2025-06-14 19:59:42 +08:00
parent 16b7f375bd
commit 77d8ed7a13
No known key found for this signature in database
9 changed files with 98 additions and 36 deletions

46
modules/nixos/default.nix Normal file
View file

@ -0,0 +1,46 @@
{ lib, config, ... }:
let
inherit (lib) mkOption;
inherit (lib.types)
listOf
str
coercedTo
submodule
;
rootDir = submodule {
options = {
directory = mkOption { type = str; };
user = mkOption {
type = str;
default = "root";
};
group = mkOption {
type = str;
default = "root";
};
mode = mkOption {
type = str;
default = "0755";
};
};
};
in
{
options = {
persistDirs = mkOption {
type = listOf (coercedTo str (d: { directory = d; }) rootDir);
default = [ ];
};
};
config = {
# Helper options
environment.persistence."/persist".directories = config.persistDirs;
# Global options
persistDirs = [
"/var/lib/systemd"
"/var/lib/nixos"
];
};
}

View file

@ -84,12 +84,9 @@ in
''; '';
programs.fuse.userAllowOther = true; programs.fuse.userAllowOther = true;
fileSystems."/persist".neededForBoot = true; fileSystems."/persist".neededForBoot = true;
#FIXME: below should be in module or something
environment.persistence."/persist" = { environment.persistence."/persist" = {
hideMounts = true; hideMounts = true;
directories = [
"/var/lib/systemd"
"/var/lib/nixos"
];
files = [ files = [
"/etc/ssh/ssh_host_ed25519_key" "/etc/ssh/ssh_host_ed25519_key"
"/etc/ssh/ssh_host_ed25519_key.pub" "/etc/ssh/ssh_host_ed25519_key.pub"

View file

@ -52,7 +52,7 @@ in
]; ];
}; };
services.fwupd.enable = true; services.fwupd.enable = true;
environment.persistence."/persist".directories = lib.singleton "/var/lib/bluetooth"; persistDirs = singleton "/var/lib/bluetooth";
hardware.bluetooth = { hardware.bluetooth = {
enable = true; enable = true;
settings.General.Experimental = true; settings.General.Experimental = true;

View file

@ -1,4 +1,7 @@
{ config, lib, ... }: { config, lib, ... }:
let
inherit (lib) singleton;
in
{ {
config = { config = {
networking = { networking = {
@ -19,6 +22,6 @@
enable = true; enable = true;
authKeyFile = config.sops.secrets."keys/tailscale".path; authKeyFile = config.sops.secrets."keys/tailscale".path;
}; };
environment.persistence."/persist".directories = [ "/var/lib/tailscale" ]; persistDirs = singleton "/var/lib/tailscale";
}; };
} }

View file

@ -5,6 +5,7 @@
... ...
}: }:
let let
inherit (lib) singleton;
cfg = config.server.databases; cfg = config.server.databases;
in in
{ {
@ -26,13 +27,11 @@ in
config = lib.mkMerge [ config = lib.mkMerge [
(lib.mkIf cfg.postgresql.enable { (lib.mkIf cfg.postgresql.enable {
networking.firewall.allowedTCPPorts = lib.singleton cfg.postgresql.port; networking.firewall.allowedTCPPorts = lib.singleton cfg.postgresql.port;
environment.persistence."/persist".directories = [ persistDirs = singleton {
{ directory = builtins.toString config.services.postgresql.dataDir;
directory = builtins.toString config.services.postgresql.dataDir; user = "postgres";
user = "postgres"; group = "postgres";
group = "postgres"; };
}
];
services.postgresql = { services.postgresql = {
enable = true; enable = true;
enableTCPIP = true; enableTCPIP = true;
@ -48,13 +47,11 @@ in
}) })
(lib.mkIf cfg.mongodb.enable { (lib.mkIf cfg.mongodb.enable {
networking.firewall.allowedTCPPorts = [ cfg.mongodb.port ]; networking.firewall.allowedTCPPorts = [ cfg.mongodb.port ];
environment.persistence."/persist".directories = [ persistDirs = singleton {
{ directory = builtins.toString config.services.mongodb.dbpath;
directory = builtins.toString config.services.mongodb.dbpath; user = "mongodb";
user = "mongodb"; group = "mongodb";
group = "mongodb"; };
}
];
services.mongodb = { services.mongodb = {
enable = true; enable = true;
bind_ip = "0.0.0.0"; bind_ip = "0.0.0.0";
@ -65,13 +62,11 @@ in
}) })
(lib.mkIf cfg.mysql.enable { (lib.mkIf cfg.mysql.enable {
networking.firewall.allowedTCPPorts = [ cfg.mysql.port ]; networking.firewall.allowedTCPPorts = [ cfg.mysql.port ];
environment.persistence."/persist".directories = [ persistDirs = singleton {
{ directory = builtins.toString config.services.mysql.dataDir;
directory = builtins.toString config.services.mysql.dataDir; user = "mysql";
user = "mysql"; group = "mysql";
group = "mysql"; };
}
];
services.mysql = { services.mysql = {
enable = true; enable = true;
package = pkgs.mariadb; package = pkgs.mariadb;

View file

@ -23,7 +23,7 @@ in
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
environment.persistence."/persist".directories = singleton { persistDirs = singleton {
directory = upstreamCfg.logDir; directory = upstreamCfg.logDir;
inherit (upstreamCfg) user group; inherit (upstreamCfg) user group;
}; };

View file

@ -33,7 +33,7 @@ in
message = "You must enable a local instance of postgresql."; message = "You must enable a local instance of postgresql.";
} }
]; ];
environment.persistence."/persist".directories = [ persistDirs = [
(mkDir cfg.configDir) (mkDir cfg.configDir)
(mkDir cfg.logDir) (mkDir cfg.logDir)
(mkDir cfg.dataDir) (mkDir cfg.dataDir)

View file

@ -0,0 +1,27 @@
{ config, lib, ... }:
let
inherit (lib) singleton mkEnableOption mkIf;
cfg = config.server.sd-webui-forge;
upstreamCfg = config.services.sd-webui-forge;
in
{
options.server.sd-webui-forge = {
enable = mkEnableOption "";
};
config = mkIf cfg.enable {
assertions = singleton {
assertion = config.hardware.gpu == "nvidia";
message = "You must run the sd-webui-forge service only with an nvidia gpu.";
};
persistDirs = singleton {
directory = upstreamCfg.dataDir;
inherit (upstreamCfg) user group;
};
services.sd-webui-forge = {
enable = true;
listen = true;
extraArgs = "--cuda-malloc";
};
};
}

View file

@ -38,11 +38,5 @@
}; };
}; };
services = { server.sd-webui-forge.enable = true;
sd-webui-forge = {
enable = true;
listen = true;
extraArgs = "--cuda-malloc";
};
};
} }