-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnix-env.nix
85 lines (69 loc) · 1.97 KB
/
nix-env.nix
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
{ pkgs ? import <nixpkgs> { overlays = [ (import ./overlay.nix) ]; } }:
let
# define the nix user UID and GID
uid = "1000";
gid = "100";
envs = pkgs.lib.fileset.toSource {
root = ./.;
fileset = ./envs;
};
in pkgs.dockerTools.buildImage {
inherit uid;
inherit gid;
name = "nix-env";
tag = "latest";
# build a base image with bash, core linux tools, nix tools, and certificates
copyToRoot = pkgs.buildEnv {
name = "env";
pathsToLink = [ "/bin" "/etc" "/envs" ];
paths =
# dockerTools helper packages
(with pkgs.dockerTools; [
caCertificates
usrBinEnv
binSh
]) ++
# minimal set of common shell requirements
(with pkgs; [
iana-etc
bashInteractive
busybox
nix
]) ++
# include example scripts to build dev environments
[ envs ];
};
# set the entrypoint, user working folder, certificates env var
# mount the home directory volume if it is used for persistence
config = {
# container will run as nix user
WorkingDir = "/home/nix";
Volumes = { "/home/nix" = { }; };
User = "nix";
# load the nix scripts at startup so that PATH is set
Cmd = [ "bash" "--rcfile" "/etc/profile.d/nix.sh" ];
# other common environment variables
Env = [
"PAGER=cat"
"USER=nix"
"NIX_PATH=nixpkgs=channel:nixos-unstable"
];
};
# finalize the image building by adding necessary components to get
# a functional nix environment: nixbld group, users, and nix.conf
runAsRoot = ''
#!${pkgs.runtimeShell}
${pkgs.dockerTools.shadowSetup}
# create the necessary groups
groupadd -g ${gid} users
# create the nix user
useradd -m -u ${uid} -g ${gid} -s /bin/bash -G users nix
# configure nix
mkdir -p /etc/nix && cat > /etc/nix/nix.conf << EOF
experimental-features = nix-command flakes
EOF
# ensure tmp exists with correct permissions
mkdir -p /tmp
chmod 1777 /tmp
'';
}