nix-config/modules/smb-mounts.nix

102 lines
2.8 KiB
Nix
Raw Normal View History

2023-09-05 21:00:29 +02:00
{
pkgs,
config,
lib,
...
}: let
inherit
(lib)
mkOption
types
flip
attrNames
2023-09-05 23:01:07 +02:00
mkMerge
concatMap
2023-09-05 23:33:38 +02:00
optional
2023-09-05 21:00:29 +02:00
;
baseOptions = [
"x-systemd.idle-timeout=60"
"x-systemd.device-timeout=5s"
"x-systemd.mount-timeout=5s"
];
in {
# Give users the ability to add their own smb shares
home-manager.sharedModules = [
{
options.home.smb = mkOption {
description = "Samba shares to be mountable under $HOME/smb";
2023-09-05 23:01:07 +02:00
default = [];
2023-09-05 23:33:38 +02:00
type = types.listOf (types.submodule ({config, ...}: {
2023-09-05 21:00:29 +02:00
options = {
localPath = mkOption {
description = "The path under which the share will be mounted. Defaults to the remotePath";
type = types.str;
2023-09-05 23:33:38 +02:00
default = config.remotePath;
2023-09-05 21:00:29 +02:00
};
address = mkOption {
description = "The remote share address";
type = types.str;
example = "10.1.2.5";
};
remotePath = mkOption {
description = "The remote share path";
type = types.str;
example = "data-10";
};
credentials = mkOption {
description = "A smb credential file to access the remote share";
type = types.path;
};
automatic = mkOption {
description = "Whether this share should be automatically mounted on boot";
default = false;
2023-09-05 23:33:38 +02:00
type = types.bool;
2023-09-05 21:00:29 +02:00
};
};
2023-09-05 23:33:38 +02:00
}));
2023-09-05 21:00:29 +02:00
};
}
];
2023-09-05 23:01:07 +02:00
imports = [
{
environment.systemPackages = [pkgs.cifs-utils];
fileSystems =
mkMerge
(
flip
concatMap
(attrNames config.home-manager.users)
(
user: let
2023-09-05 23:33:38 +02:00
parentPath = "/home/${user}/smb";
2023-09-05 23:01:07 +02:00
cfg = config.home-manager.users.${user}.home.smb;
2023-09-05 23:33:38 +02:00
inherit (config.users.users.${user}) uid;
inherit (config.users.groups.${user}) gid;
2023-09-05 23:01:07 +02:00
in
flip map cfg (
cfg: {
2023-09-05 23:33:38 +02:00
"${parentPath}/${cfg.localPath}" = let
2023-09-05 23:01:07 +02:00
options =
baseOptions
++ [
2023-09-05 23:33:38 +02:00
"uid=${toString uid}"
"gid=${toString gid}"
2024-01-15 21:46:10 +01:00
"file_mode=0600"
"dir_mode=0700"
2023-09-05 23:01:07 +02:00
"credentials=${cfg.credentials}"
2023-09-05 23:33:38 +02:00
]
++ (optional (!cfg.automatic) "noauto");
2023-09-05 23:01:07 +02:00
in {
inherit options;
device = "//${cfg.address}/${cfg.remotePath}";
fsType = "cifs";
};
}
)
)
);
}
];
2023-09-05 21:00:29 +02:00
}