2
2
3
3
let
4
4
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 ;
8
10
types = lib . types ;
9
11
envContainerName = builtins . getEnv "DEVENV_CONTAINER" ;
10
12
20
22
url = "github:rrbutani/nix-mk-shell-bin" ;
21
23
attribute = "containers" ;
22
24
} ;
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
+ } ;
24
29
bash = "${ pkgs . bashInteractive } /bin/bash" ;
25
- mkEntrypoint = cfg : pkgs . writeScript "entrypoint" ''
26
- #!${ bash }
30
+ mkEntrypoint = cfg :
31
+ pkgs . writeScript "entrypoint" ''
32
+ #!${ bash }
27
33
28
- export PATH=/bin
34
+ export PATH=/bin
29
35
30
- source ${ shell . envScript }
36
+ source ${ shell . envScript }
31
37
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`"
34
40
35
- ${ bash } -c "$cmd"
36
- '' ;
41
+ ${ bash } -c "$cmd"
42
+ '' ;
37
43
user = "user" ;
38
44
group = "user" ;
39
45
uid = "1000" ;
40
46
gid = "1000" ;
41
47
homeDir = "/env" ;
42
48
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
+ '' ) ;
47
54
48
55
mkMultiHome = paths : map mkHome paths ;
49
56
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 ] ) ;
55
62
56
63
mkTmp = ( pkgs . runCommand "devenv-container-tmp" { } ''
57
64
mkdir -p $out/tmp
@@ -81,101 +88,102 @@ let
81
88
touch $out/etc/login.defs
82
89
'' ) ;
83
90
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
+ } ;
94
99
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 [ {
128
129
path = mkTmp ;
129
130
regex = "/tmp" ;
130
131
mode = "1777" ;
131
132
uid = 0 ;
132
133
gid = 0 ;
133
134
uname = "root" ;
134
135
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
+ ] ;
148
151
} ;
149
- } ;
150
152
151
153
# <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
+ '' ;
179
187
containerOptions = types . submodule ( { name , config , ... } : {
180
188
options = {
181
189
name = lib . mkOption {
193
201
194
202
copyToRoot = lib . mkOption {
195
203
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." ;
197
206
default = self ;
198
207
defaultText = "self" ;
199
208
} ;
@@ -213,11 +222,10 @@ let
213
222
214
223
defaultCopyArgs = lib . mkOption {
215
224
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
+ '' ;
221
229
default = [ ] ;
222
230
} ;
223
231
236
244
isBuilding = lib . mkOption {
237
245
type = types . bool ;
238
246
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)." ;
240
255
} ;
241
256
242
257
derivation = lib . mkOption {
266
281
containers = lib . mkOption {
267
282
type = types . attrsOf containerOptions ;
268
283
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`." ;
270
286
} ;
271
287
272
288
container = {
273
289
isBuilding = lib . mkOption {
274
290
type = types . bool ;
275
291
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." ;
277
294
} ;
278
295
} ;
279
296
} ;
292
309
startupCommand = lib . mkDefault config . procfileScript ;
293
310
} ;
294
311
}
295
- ( if envContainerName == "" then { } else {
312
+ ( if envContainerName == "" then
313
+ { }
314
+ else {
296
315
containers . ${ envContainerName } . isBuilding = true ;
297
316
} )
298
317
( 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" ;
301
322
devenv . root = lib . mkForce "${ homeDir } " ;
302
323
devenv . dotfile = lib . mkOverride 49 "${ homeDir } /.devenv" ;
303
324
} )
0 commit comments