nix-config/modules/smb-mounts.nix

105 lines
2.8 KiB
Nix
Raw Normal View History

2023-09-05 21:00:29 +02:00
{
pkgs,
config,
lib,
...
2024-07-26 22:12:48 +02:00
}:
let
inherit (lib)
2023-09-05 21:00:29 +02:00
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"
];
2024-07-26 22:12:48 +02:00
in
{
2023-09-05 21:00:29 +02:00
# 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";
2024-07-26 22:12:48 +02:00
default = [ ];
type = types.listOf (
types.submodule (
{ config, ... }:
{
options = {
localPath = mkOption {
description = "The path under which the share will be mounted. Defaults to the remotePath";
type = types.str;
default = config.remotePath;
};
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;
type = types.bool;
};
};
}
)
);
2023-09-05 21:00:29 +02:00
};
}
];
2023-09-05 23:01:07 +02:00
imports = [
{
2024-07-26 22:12:48 +02:00
environment.systemPackages = [ pkgs.cifs-utils ];
fileSystems = mkMerge (
flip concatMap (attrNames config.home-manager.users) (
user:
let
parentPath = "/home/${user}/smb";
cfg = config.home-manager.users.${user}.home.smb;
inherit (config.users.users.${user}) uid;
inherit (config.users.groups.${user}) gid;
in
flip map cfg (cfg: {
"${parentPath}/${cfg.localPath}" =
let
options =
baseOptions
++ [
"uid=${toString uid}"
"gid=${toString gid}"
"file_mode=0600"
"dir_mode=0700"
"credentials=${cfg.credentials}"
"x-systemd.after=network-online.target"
2024-07-26 22:12:48 +02:00
]
++ (optional (!cfg.automatic) "noauto");
in
{
inherit options;
device = "//${cfg.address}/${cfg.remotePath}";
fsType = "cifs";
};
})
)
);
2023-09-05 23:01:07 +02:00
}
];
2023-09-05 21:00:29 +02:00
}