2024-04-25 02:20:11 +02:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}:
|
2018-03-12 05:13:31 +01:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.programs.singularity;
|
2023-01-28 18:02:47 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
|
2018-03-12 05:13:31 +01:00
|
|
|
options.programs.singularity = {
|
2024-04-13 14:54:15 +02:00
|
|
|
enable = mkEnableOption "singularity" // {
|
|
|
|
description = ''
|
2023-01-28 18:02:47 +01:00
|
|
|
Whether to install Singularity/Apptainer with system-level overriding such as SUID support.
|
|
|
|
'';
|
|
|
|
};
|
2024-04-25 02:20:11 +02:00
|
|
|
package = mkPackageOption pkgs "singularity" { example = "apptainer"; };
|
2023-01-28 18:02:47 +01:00
|
|
|
packageOverriden = mkOption {
|
|
|
|
type = types.nullOr types.package;
|
|
|
|
default = null;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2023-05-20 04:11:38 +02:00
|
|
|
This option provides access to the overridden result of `programs.singularity.package`.
|
2023-01-28 18:02:47 +01:00
|
|
|
|
2023-05-20 04:11:38 +02:00
|
|
|
For example, the following configuration makes all the Nixpkgs packages use the overridden `singularity`:
|
2023-01-28 18:02:47 +01:00
|
|
|
```Nix
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
{
|
|
|
|
nixpkgs.overlays = [
|
|
|
|
(final: prev: {
|
|
|
|
_singularity-orig = prev.singularity;
|
|
|
|
singularity = config.programs.singularity.packageOverriden;
|
|
|
|
})
|
|
|
|
];
|
|
|
|
programs.singularity.enable = true;
|
|
|
|
programs.singularity.package = pkgs._singularity-orig;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-05-20 04:11:38 +02:00
|
|
|
Use `lib.mkForce` to forcefully specify the overridden package.
|
2023-01-28 18:02:47 +01:00
|
|
|
'';
|
|
|
|
};
|
2023-08-22 12:05:31 +02:00
|
|
|
enableExternalLocalStateDir = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
example = false;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2023-08-22 12:05:31 +02:00
|
|
|
Whether to use top-level directories as LOCALSTATEDIR
|
|
|
|
instead of the store path ones.
|
|
|
|
This affects the SESSIONDIR of Apptainer/Singularity.
|
|
|
|
If set to true, the SESSIONDIR will become
|
|
|
|
`/var/lib/''${projectName}/mnt/session`.
|
|
|
|
'';
|
|
|
|
};
|
2023-01-28 18:02:48 +01:00
|
|
|
enableFakeroot = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
example = false;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2023-01-28 18:02:48 +01:00
|
|
|
Whether to enable the `--fakeroot` support of Singularity/Apptainer.
|
|
|
|
'';
|
|
|
|
};
|
2023-01-28 18:02:47 +01:00
|
|
|
enableSuid = mkOption {
|
|
|
|
type = types.bool;
|
2024-01-03 23:28:57 +01:00
|
|
|
# SingularityCE requires SETUID for most things. Apptainer prefers user
|
|
|
|
# namespaces, e.g. `apptainer exec --nv` would fail if built
|
|
|
|
# `--with-suid`:
|
|
|
|
# > `FATAL: nvidia-container-cli not allowed in setuid mode`
|
|
|
|
default = cfg.package.projectName != "apptainer";
|
|
|
|
defaultText = literalExpression ''config.services.singularity.package.projectName != "apptainer"'';
|
2023-01-28 18:02:47 +01:00
|
|
|
example = false;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2023-01-28 18:02:47 +01:00
|
|
|
Whether to enable the SUID support of Singularity/Apptainer.
|
|
|
|
'';
|
|
|
|
};
|
2018-03-12 05:13:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2024-04-25 02:20:11 +02:00
|
|
|
programs.singularity.packageOverriden = (
|
|
|
|
cfg.package.override (
|
|
|
|
optionalAttrs cfg.enableExternalLocalStateDir { externalLocalStateDir = "/var/lib"; }
|
|
|
|
// optionalAttrs cfg.enableFakeroot {
|
|
|
|
newuidmapPath = "/run/wrappers/bin/newuidmap";
|
|
|
|
newgidmapPath = "/run/wrappers/bin/newgidmap";
|
|
|
|
}
|
|
|
|
// optionalAttrs cfg.enableSuid {
|
|
|
|
enableSuid = true;
|
|
|
|
starterSuidPath = "/run/wrappers/bin/${cfg.package.projectName}-suid";
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
2023-01-28 18:02:47 +01:00
|
|
|
environment.systemPackages = [ cfg.packageOverriden ];
|
|
|
|
security.wrappers."${cfg.packageOverriden.projectName}-suid" = mkIf cfg.enableSuid {
|
|
|
|
setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
|
|
|
source = "${cfg.packageOverriden}/libexec/${cfg.packageOverriden.projectName}/bin/starter-suid.orig";
|
|
|
|
};
|
2023-08-22 12:05:31 +02:00
|
|
|
systemd.tmpfiles.rules = mkIf cfg.enableExternalLocalStateDir [
|
2023-01-28 18:02:47 +01:00
|
|
|
"d /var/lib/${cfg.packageOverriden.projectName}/mnt/session 0770 root root -"
|
2023-01-28 18:02:47 +01:00
|
|
|
];
|
2018-03-12 05:13:31 +01:00
|
|
|
};
|
|
|
|
}
|