Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions nix/modules/darwin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
if cfg.darwin.paths.blocksRoot != null
then cfg.darwin.paths.blocksRoot
else "${stateDir}/blocks";
fileLoggingEnabled = cfg.configSchema.logTarget == "file" || cfg.configSchema.logTarget == "both";
logsPath =
if cfg.darwin.paths.logsDir != null
then cfg.darwin.paths.logsDir
else defaultLogsPath;
else if fileLoggingEnabled
then defaultLogsPath
else "${stateDir}/logs";
configSource = shared.mkConfigToml {
inherit cfg;
inherit accountsPath;
Expand Down Expand Up @@ -103,14 +106,10 @@ in {
];

config = lib.mkIf cfg.enable {
assertions = [
{
assertion = !(cfg.configFile != null && cfg.environmentFile != null);
message = "services.mithril.configFile and services.mithril.environmentFile cannot both be set.";
}
];
warnings =
lib.optional (!darwinExternalDiskUsed)
lib.optional (cfg.configFile != null && cfg.environmentFile != null)
(builtins.throw "services.mithril.configFile and services.mithril.environmentFile cannot both be set.")
++ lib.optional (!darwinExternalDiskUsed)
"Mithril on macOS writes heavily; use external storage (e.g. /Volumes/...) via services.mithril.darwin.paths.* or services.mithril.configSchema.storage* to reduce SSD wear.";

services.mithril.configFile = lib.mkForce configFile;
Expand Down
21 changes: 12 additions & 9 deletions nix/modules/home-manager/service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,28 @@
if pkgs.stdenv.isDarwin && cfg.darwin.paths.blocksRoot != null
then cfg.darwin.paths.blocksRoot
else "${stateDir}/blocks";
fileLoggingEnabled = cfg.configSchema.logTarget == "file" || cfg.configSchema.logTarget == "both";
logsPath =
if pkgs.stdenv.isDarwin && cfg.darwin.paths.logsDir != null
if !fileLoggingEnabled
then null
else if pkgs.stdenv.isDarwin && cfg.darwin.paths.logsDir != null
then cfg.darwin.paths.logsDir
else cfg.configSchema.storageLogs;
shared = import ../shared/lib.nix {inherit lib pkgs;};
configTemplate = shared.mkConfigTomlTemplate {
inherit cfg;
accountsPath = "@STATE_DIRECTORY@/accounts";
blocksRoot = "@STATE_DIRECTORY@/blocks";
logsPath = "@LOGS_DIRECTORY@";
logsPath =
if fileLoggingEnabled
then "@LOGS_DIRECTORY@"
else "@STATE_DIRECTORY@/logs";
};
configInitScript = pkgs.writeShellScript "mithril-generate-config-user" ''
set -euo pipefail
config_dir="$CONFIGURATION_DIRECTORY"
state_dir="$STATE_DIRECTORY"
logs_dir="$LOGS_DIRECTORY"
logs_dir="''${LOGS_DIRECTORY:-}"
runtime_dir="$RUNTIME_DIRECTORY"
mkdir -p "$config_dir"
mkdir -p "$runtime_dir"
Expand Down Expand Up @@ -132,12 +138,9 @@ in {
config = lib.mkIf cfg.enable (
lib.mkMerge [
{
assertions = [
{
assertion = !(cfg.configFile != null && cfg.environmentFile != null);
message = "services.mithril.configFile and services.mithril.environmentFile cannot both be set.";
}
];
warnings =
lib.optional (cfg.configFile != null && cfg.environmentFile != null)
(builtins.throw "services.mithril.configFile and services.mithril.environmentFile cannot both be set.");
}
(lib.mkIf (effectiveGenerate && pkgs.stdenv.isDarwin) {
xdg.configFile."mithril/config.toml".source = shared.mkConfigToml {
Expand Down
55 changes: 45 additions & 10 deletions nix/modules/nixos/service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,61 @@
if cfg.storage.singleDisk.mountPoint != null
then "${escapeSystemdPath cfg.storage.singleDisk.mountPoint}.mount"
else null;
fileLoggingEnabled = cfg.configSchema.logTarget == "file" || cfg.configSchema.logTarget == "both";
hasExternalStorage =
cfg.storage.singleDisk.enable
|| cfg.storage.accounts.device != null
|| cfg.storage.blocks.device != null;
shared = import ../shared/lib.nix {inherit lib pkgs;};
configTemplate = shared.mkConfigTomlTemplate {
inherit cfg;
accountsPath = "@STATE_DIRECTORY@/accounts";
blocksRoot = "@STATE_DIRECTORY@/blocks";
logsPath = "@LOGS_DIRECTORY@";
accountsPath =
if cfg.storage.accounts.mountPoint != null
then cfg.storage.accounts.mountPoint
else "@STATE_DIRECTORY@/accounts";
blocksRoot =
if cfg.storage.blocks.mountPoint != null
then cfg.storage.blocks.mountPoint
else "@STATE_DIRECTORY@/blocks";
logsPath =
if fileLoggingEnabled
then "@LOGS_DIRECTORY@"
else "@STATE_DIRECTORY@/logs";
};
mkdirsScript = pkgs.writeShellScript "mithril-mkdirs" ''
set -euo pipefail
${lib.optionalString hasExternalStorage ''
# External storage mounts are root-owned after mkfs.
# Chown mount point roots so the DynamicUser can write.
if [ -n "${singleDiskMountPoint}" ]; then
chown "${cfg.user}:${cfg.group}" "${singleDiskMountPoint}"
fi
${lib.optionalString (cfg.storage.accounts.device != null) ''
if [ -n "${accountsMountPoint}" ]; then
chown "${cfg.user}:${cfg.group}" "${accountsMountPoint}"
fi
''}
${lib.optionalString (cfg.storage.blocks.device != null) ''
if [ -n "${blocksMountPoint}" ]; then
chown "${cfg.user}:${cfg.group}" "${blocksMountPoint}"
fi
''}
''}
if [ -n "${accountsMountPoint}" ]; then
install -d -m 0755 -o ${cfg.user} -g ${cfg.group} "${accountsMountPoint}"
install -d -m 0755 ${lib.optionalString hasExternalStorage "-o ${cfg.user} -g ${cfg.group}"} "${accountsMountPoint}"
fi
if [ -n "${blocksMountPoint}" ]; then
install -d -m 0755 -o ${cfg.user} -g ${cfg.group} "${blocksMountPoint}"
install -d -m 0755 ${lib.optionalString hasExternalStorage "-o ${cfg.user} -g ${cfg.group}"} "${blocksMountPoint}"
fi
if [ -n "${logsMountPoint}" ]; then
install -d -m 0755 -o ${cfg.user} -g ${cfg.group} "${logsMountPoint}"
install -d -m 0755 ${lib.optionalString hasExternalStorage "-o ${cfg.user} -g ${cfg.group}"} "${logsMountPoint}"
fi
'';
configInitScript = pkgs.writeShellScript "mithril-generate-config" ''
set -euo pipefail
config_dir="$CONFIGURATION_DIRECTORY"
state_dir="$STATE_DIRECTORY"
logs_dir="$LOGS_DIRECTORY"
logs_dir="''${LOGS_DIRECTORY:-}"
runtime_dir="$RUNTIME_DIRECTORY"
mkdir -p "$config_dir"
mkdir -p "$runtime_dir"
Expand Down Expand Up @@ -90,6 +121,10 @@
' "$config_dir/config.toml" > "$runtime_dir/config.toml.tmp"
mv "$runtime_dir/config.toml.tmp" "$config_dir/config.toml"
'';
singleDiskMountPoint =
if cfg.storage.singleDisk.mountPoint != null
then cfg.storage.singleDisk.mountPoint
else "";
accountsMountPoint =
if cfg.storage.accounts.mountPoint != null
then cfg.storage.accounts.mountPoint
Expand All @@ -99,7 +134,7 @@
then cfg.storage.blocks.mountPoint
else "";
logsMountPoint =
if cfg.configSchema.storageLogs != null
if fileLoggingEnabled && cfg.configSchema.storageLogs != null
then cfg.configSchema.storageLogs
else "";
in {
Expand Down Expand Up @@ -131,8 +166,8 @@ in {
++ lib.optional (cfg.storage.blocks.device != null && blocksMountUnit != null) blocksMountUnit;

serviceConfig.ExecStartPre =
[mkdirsScript]
++ lib.optionals (effectiveGenerate && cfg.configFile == null) [configInitScript];
["+${mkdirsScript}"]
++ lib.optionals (effectiveGenerate && cfg.configFile == null) ["+${configInitScript}"];
};

mithril-thp = lib.mkIf cfg.performance.enable {
Expand Down
Loading
Loading