nix-config/modules/smb-mounts.nix

113 lines
3.1 KiB
Nix
Raw Permalink 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)
2024-11-26 18:14:27 +01:00
any
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
};
}
];
2024-11-26 18:14:27 +01:00
imports =
let
existingCfg = flip any (attrNames config.home-manager.users) (
user: (config.home-manager.users.${user}.home.smb != [ ])
2024-07-26 22:12:48 +02:00
);
2024-11-26 18:14:27 +01:00
in
[
{
environment.systemPackages = lib.optional existingCfg 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.automount"
"_netdev"
]
++ (optional (!cfg.automatic) "noauto");
in
{
inherit options;
device = "//${cfg.address}/${cfg.remotePath}";
fsType = "cifs";
};
})
)
);
}
];
2023-09-05 21:00:29 +02:00
}