blob: 9e8073c4d3aeef5215abadf081634b1625beeaa8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
{
config,
lib,
pkgs,
...
}:
let
cfg = config.programs.nix-required-mounts;
package = pkgs.nix-required-mounts;
Mount =
with lib;
types.submodule {
options.host = mkOption {
type = types.str;
description = "Host path to mount";
};
options.guest = mkOption {
type = types.str;
description = "Location in the sandbox to mount the host path at";
};
};
Pattern =
with lib.types;
types.submodule (
{ config, name, ... }:
{
options.onFeatures = lib.mkOption {
type = listOf types.str;
description = "Which requiredSystemFeatures should trigger relaxation of the sandbox";
default = [ name ];
};
options.paths = lib.mkOption {
type = listOf (oneOf [
path
Mount
]);
description = "A list of glob patterns, indicating which paths to expose to the sandbox";
};
options.unsafeFollowSymlinks = lib.mkEnableOption ''
Instructs the hook to mount the symlink targets as well, when any of
the `paths` contain symlinks. This may not work correctly with glob
patterns.
'';
options.safePrefixes = lib.mkOption {
default = [ builtins.storeDir ];
type = listOf path;
description = "A list of path prefixes that do not need and shall not be searched recursively for further symlink targets. Everything in the nix store does not need to be searched as the derivation already calculcated the full closure of all nix store paths for the drivers package.";
};
}
);
driverPaths = [
pkgs.addDriverRunpath.driverLink
# mesa:
config.hardware.graphics.package
# nvidia_x11, etc:
]
++ config.hardware.graphics.extraPackages; # nvidia_x11
defaults = {
nvidia-gpu.onFeatures = package.allowedPatterns.nvidia-gpu.onFeatures;
nvidia-gpu.paths = package.allowedPatterns.nvidia-gpu.paths ++ driverPaths;
# TODO: Refactor `hardware.graphics` to ease referencing the closure
# NOTE: A naive implementation may e.g. introduce a conditional infinite recursion (https://github.com/NixOS/nixpkgs/pull/488199)
nvidia-gpu.unsafeFollowSymlinks = true;
zluda = {
onFeatures = [
"amd-gpu"
"cuda"
"gpu"
"opengl"
];
paths = [
pkgs.addDriverRunpath.driverLink
"/dev/dri"
"/dev/kfd"
"/sys/devices/virtual/kfd"
# As per https://www.kernel.org/doc/Documentation/admin-guide/devices.txt
# 226 is the major ID for "Direct Rendering Infrastructure (DRI)" devices
"/sys/dev/char/226:*"
]
++ config.hardware.graphics.extraPackages;
unsafeFollowSymlinks = true;
};
};
in
{
meta.maintainers = with lib.maintainers; [ SomeoneSerge ];
options.programs.nix-required-mounts = {
enable = lib.mkEnableOption "Expose extra paths to the sandbox depending on derivations' requiredSystemFeatures";
presets.nvidia-gpu.enable = lib.mkEnableOption ''
Declare the support for derivations that require an Nvidia GPU to be
available, e.g. derivations with `requiredSystemFeatures = [ "cuda" ]`.
This mounts the corresponding userspace drivers and device nodes in the
sandbox, but only for derivations that request these special features.
You may extend or override the exposed paths via the
`programs.nix-required-mounts.allowedPatterns.nvidia-gpu.paths` option.
'';
presets.zluda.enable = lib.mkEnableOption ''
Same as `programs.nix-required-mounts.presets.nvidia-gpu` but adds paths
to the sandbox that are needed for running CUDA applications on top of
the ZLUDA translation layer combined with AMD GPUs.
You may extend or override the exposed paths via the
`programs.nix-required-mounts.allowedPatterns.zluda.paths` option.
'';
allowedPatterns =
with lib.types;
lib.mkOption {
type = attrsOf Pattern;
description = "The hook config, describing which paths to mount for which system features";
default = { };
defaultText = lib.literalExpression ''
{
opengl.paths = config.hardware.graphics.extraPackages ++ [
config.graphics.opengl.package
pkgs.addDriverRunpath.driverLink
"/dev/dri"
];
}
'';
example.require-ipfs.paths = [ "/ipfs" ];
example.require-ipfs.onFeatures = [ "ipfs" ];
};
extraWrapperArgs = lib.mkOption {
type = with lib.types; listOf str;
default = [ ];
description = "List of extra arguments (such as `--add-flags -v`) to pass to the hook's wrapper";
};
package = lib.mkOption {
type = lib.types.package;
default = package.override { inherit (cfg) allowedPatterns extraWrapperArgs; };
description = "The final package with the final config applied";
internal = true;
};
};
config = lib.mkIf cfg.enable (
lib.mkMerge [
{ nix.settings.pre-build-hook = lib.getExe cfg.package; }
(lib.mkIf cfg.presets.nvidia-gpu.enable {
hardware.graphics.enable = lib.mkDefault true;
nix.settings.system-features = cfg.allowedPatterns.nvidia-gpu.onFeatures;
programs.nix-required-mounts.allowedPatterns = {
inherit (defaults) nvidia-gpu;
};
})
(lib.mkIf cfg.presets.zluda.enable {
hardware.graphics.enable = lib.mkDefault true;
hardware.amdgpu.zluda.enable = lib.mkDefault true;
nix.settings.system-features = cfg.allowedPatterns.zluda.onFeatures;
programs.nix-required-mounts.allowedPatterns = {
inherit (defaults) zluda;
};
})
]
);
}
|