Skip to content

Commit 14b40b5

Browse files
committed
feat: add flag to disable containers tooling (#1367)
1 parent 25e48e9 commit 14b40b5

File tree

1 file changed

+138
-117
lines changed

1 file changed

+138
-117
lines changed

src/modules/containers.nix

+138-117
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
let
44
projectName = name:
5-
if config.name == null
6-
then throw ''You need to set `name = "myproject";` or `containers.${name}.name = "mycontainer"; to be able to generate a container.''
7-
else config.name;
5+
if config.name == null then
6+
throw ''
7+
You need to set `name = "myproject";` or `containers.${name}.name = "mycontainer"; to be able to generate a container.''
8+
else
9+
config.name;
810
types = lib.types;
911
envContainerName = builtins.getEnv "DEVENV_CONTAINER";
1012

@@ -20,38 +22,43 @@ let
2022
url = "github:rrbutani/nix-mk-shell-bin";
2123
attribute = "containers";
2224
};
23-
shell = mk-shell-bin.lib.mkShellBin { drv = config.shell; nixpkgs = pkgs; };
25+
shell = mk-shell-bin.lib.mkShellBin {
26+
drv = config.shell;
27+
nixpkgs = pkgs;
28+
};
2429
bash = "${pkgs.bashInteractive}/bin/bash";
25-
mkEntrypoint = cfg: pkgs.writeScript "entrypoint" ''
26-
#!${bash}
30+
mkEntrypoint = cfg:
31+
pkgs.writeScript "entrypoint" ''
32+
#!${bash}
2733
28-
export PATH=/bin
34+
export PATH=/bin
2935
30-
source ${shell.envScript}
36+
source ${shell.envScript}
3137
32-
# expand any envvars before exec
33-
cmd="`echo "$@"|${pkgs.envsubst}/bin/envsubst`"
38+
# expand any envvars before exec
39+
cmd="`echo "$@"|${pkgs.envsubst}/bin/envsubst`"
3440
35-
${bash} -c "$cmd"
36-
'';
41+
${bash} -c "$cmd"
42+
'';
3743
user = "user";
3844
group = "user";
3945
uid = "1000";
4046
gid = "1000";
4147
homeDir = "/env";
4248

43-
mkHome = path: (pkgs.runCommand "devenv-container-home" { } ''
44-
mkdir -p $out${homeDir}
45-
cp -R ${path}/* $out${homeDir}/
46-
'');
49+
mkHome = path:
50+
(pkgs.runCommand "devenv-container-home" { } ''
51+
mkdir -p $out${homeDir}
52+
cp -R ${path}/* $out${homeDir}/
53+
'');
4754

4855
mkMultiHome = paths: map mkHome paths;
4956

50-
homeRoots = cfg: (
51-
if (builtins.typeOf cfg.copyToRoot == "list")
52-
then cfg.copyToRoot
53-
else [ cfg.copyToRoot ]
54-
);
57+
homeRoots = cfg:
58+
(if (builtins.typeOf cfg.copyToRoot == "list") then
59+
cfg.copyToRoot
60+
else
61+
[ cfg.copyToRoot ]);
5562

5663
mkTmp = (pkgs.runCommand "devenv-container-tmp" { } ''
5764
mkdir -p $out/tmp
@@ -81,101 +88,102 @@ let
8188
touch $out/etc/login.defs
8289
'');
8390

84-
mkPerm = derivation:
85-
{
86-
path = derivation;
87-
mode = "0744";
88-
uid = lib.toInt uid;
89-
gid = lib.toInt gid;
90-
uname = user;
91-
gname = group;
92-
};
93-
91+
mkPerm = derivation: {
92+
path = derivation;
93+
mode = "0744";
94+
uid = lib.toInt uid;
95+
gid = lib.toInt gid;
96+
uname = user;
97+
gname = group;
98+
};
9499

95-
mkDerivation = cfg: nix2container.nix2container.buildImage {
96-
name = cfg.name;
97-
tag = cfg.version;
98-
initializeNixDatabase = true;
99-
nixUid = lib.toInt uid;
100-
nixGid = lib.toInt gid;
101-
102-
copyToRoot = [
103-
(pkgs.buildEnv {
104-
name = "devenv-container-root";
105-
paths = [
106-
pkgs.coreutils-full
107-
pkgs.bashInteractive
108-
pkgs.su
109-
pkgs.sudo
110-
];
111-
pathsToLink = "/bin";
112-
})
113-
mkEtc
114-
mkTmp
115-
];
116-
117-
maxLayers = cfg.maxLayers;
118-
119-
layers = [
120-
(nix2container.nix2container.buildLayer {
121-
perms = map mkPerm (mkMultiHome (homeRoots cfg));
122-
copyToRoot = mkMultiHome (homeRoots cfg);
123-
})
124-
];
125-
126-
perms = [
127-
{
100+
mkDerivation = cfg:
101+
nix2container.nix2container.buildImage {
102+
name = cfg.name;
103+
tag = cfg.version;
104+
initializeNixDatabase = cfg.isDev;
105+
nixUid = lib.toInt uid;
106+
nixGid = lib.toInt gid;
107+
108+
copyToRoot = lib.lists.optionals cfg.isDev [
109+
(pkgs.buildEnv {
110+
name = "devenv-container-root";
111+
paths =
112+
[ pkgs.coreutils-full pkgs.bashInteractive pkgs.su pkgs.sudo ];
113+
pathsToLink = "/bin";
114+
})
115+
mkEtc
116+
mkTmp
117+
];
118+
119+
maxLayers = cfg.maxLayers;
120+
121+
layers = [
122+
(nix2container.nix2container.buildLayer {
123+
perms = map mkPerm (mkMultiHome (homeRoots cfg));
124+
copyToRoot = mkMultiHome (homeRoots cfg);
125+
})
126+
];
127+
128+
perms = lib.lists.optionals cfg.isDev [{
128129
path = mkTmp;
129130
regex = "/tmp";
130131
mode = "1777";
131132
uid = 0;
132133
gid = 0;
133134
uname = "root";
134135
gname = "root";
135-
}
136-
];
137-
138-
config = {
139-
Entrypoint = cfg.entrypoint;
140-
User = "${user}";
141-
WorkingDir = "${homeDir}";
142-
Env = lib.mapAttrsToList
143-
(name: value:
144-
"${name}=${toString value}"
145-
)
146-
config.env ++ [ "HOME=${homeDir}" "USER=${user}" ];
147-
Cmd = [ cfg.startupCommand ];
136+
}];
137+
138+
config = lib.attrsets.mergeAttrsList [
139+
{
140+
User = "${user}";
141+
WorkingDir = "${homeDir}";
142+
}
143+
(if cfg.isDev then {
144+
Env = lib.mapAttrsToList (name: value: "${name}=${toString value}")
145+
config.env ++ [ "HOME=${homeDir}" "USER=${user}" ];
146+
Entrypoint = cfg.entrypoint;
147+
Cmd = [ cfg.startupCommand ];
148+
} else
149+
{ })
150+
];
148151
};
149-
};
150152

151153
# <registry> <args>
152-
mkCopyScript = cfg: pkgs.writeShellScript "copy-container" ''
153-
set -e -o pipefail
154-
155-
container=$1
156-
shift
157-
158-
if [[ "$1" == false ]]; then
159-
registry=${cfg.registry}
160-
else
161-
registry="$1"
162-
fi
163-
shift
164-
165-
dest="''${registry}${cfg.name}:${cfg.version}"
166-
167-
if [[ $# == 0 ]]; then
168-
args=(${if cfg.defaultCopyArgs == [] then "" else toString cfg.defaultCopyArgs})
169-
else
170-
args=("$@")
171-
fi
172-
173-
echo
174-
echo "Copying container $container to $dest"
175-
echo
176-
177-
${nix2container.skopeo-nix2container}/bin/skopeo --insecure-policy copy "nix:$container" "$dest" ''${args[@]}
178-
'';
154+
mkCopyScript = cfg:
155+
pkgs.writeShellScript "copy-container" ''
156+
set -e -o pipefail
157+
158+
container=$1
159+
shift
160+
161+
if [[ "$1" == false ]]; then
162+
registry=${cfg.registry}
163+
else
164+
registry="$1"
165+
fi
166+
shift
167+
168+
dest="''${registry}${cfg.name}:${cfg.version}"
169+
170+
if [[ $# == 0 ]]; then
171+
args=(${
172+
if cfg.defaultCopyArgs == [ ] then
173+
""
174+
else
175+
toString cfg.defaultCopyArgs
176+
})
177+
else
178+
args=("$@")
179+
fi
180+
181+
echo
182+
echo "Copying container $container to $dest"
183+
echo
184+
185+
${nix2container.skopeo-nix2container}/bin/skopeo --insecure-policy copy "nix:$container" "$dest" ''${args[@]}
186+
'';
179187
containerOptions = types.submodule ({ name, config, ... }: {
180188
options = {
181189
name = lib.mkOption {
@@ -193,7 +201,8 @@ let
193201

194202
copyToRoot = lib.mkOption {
195203
type = types.either types.path (types.listOf types.path);
196-
description = "Add a path to the container. Defaults to the whole git repo.";
204+
description =
205+
"Add a path to the container. Defaults to the whole git repo.";
197206
default = self;
198207
defaultText = "self";
199208
};
@@ -213,11 +222,10 @@ let
213222

214223
defaultCopyArgs = lib.mkOption {
215224
type = types.listOf types.str;
216-
description =
217-
''
218-
Default arguments to pass to `skopeo copy`.
219-
You can override them by passing arguments to the script.
220-
'';
225+
description = ''
226+
Default arguments to pass to `skopeo copy`.
227+
You can override them by passing arguments to the script.
228+
'';
221229
default = [ ];
222230
};
223231

@@ -236,7 +244,14 @@ let
236244
isBuilding = lib.mkOption {
237245
type = types.bool;
238246
default = false;
239-
description = "Set to true when the environment is building this container.";
247+
description =
248+
"Set to true when the environment is building this container.";
249+
};
250+
251+
isDev = lib.mkOption {
252+
type = types.bool;
253+
default = true;
254+
description = "Is a development containers (add tools).";
240255
};
241256

242257
derivation = lib.mkOption {
@@ -266,14 +281,16 @@ in
266281
containers = lib.mkOption {
267282
type = types.attrsOf containerOptions;
268283
default = { };
269-
description = "Container specifications that can be built, copied and ran using `devenv container`.";
284+
description =
285+
"Container specifications that can be built, copied and ran using `devenv container`.";
270286
};
271287

272288
container = {
273289
isBuilding = lib.mkOption {
274290
type = types.bool;
275291
default = false;
276-
description = "Set to true when the environment is building a container.";
292+
description =
293+
"Set to true when the environment is building a container.";
277294
};
278295
};
279296
};
@@ -292,12 +309,16 @@ in
292309
startupCommand = lib.mkDefault config.procfileScript;
293310
};
294311
}
295-
(if envContainerName == "" then { } else {
312+
(if envContainerName == "" then
313+
{ }
314+
else {
296315
containers.${envContainerName}.isBuilding = true;
297316
})
298317
(lib.mkIf config.container.isBuilding {
299-
devenv.tmpdir = lib.mkOverride (lib.modules.defaultOverridePriority - 1) "/tmp";
300-
devenv.runtime = lib.mkOverride (lib.modules.defaultOverridePriority - 1) "${config.devenv.tmpdir}/devenv";
318+
devenv.tmpdir =
319+
lib.mkOverride (lib.modules.defaultOverridePriority - 1) "/tmp";
320+
devenv.runtime = lib.mkOverride (lib.modules.defaultOverridePriority - 1)
321+
"${config.devenv.tmpdir}/devenv";
301322
devenv.root = lib.mkForce "${homeDir}";
302323
devenv.dotfile = lib.mkOverride 49 "${homeDir}/.devenv";
303324
})

0 commit comments

Comments
 (0)