|
58 | 58 | zstd
|
59 | 59 | ];
|
60 | 60 |
|
61 |
| - # Start with an Ubuntu image! |
| 61 | + # This is the base image for our Docker container used for development. |
| 62 | + # Use `nix-prefetch-docker ubuntu --arch amd64 --image-tag lunar` to get this. |
62 | 63 | baseDevEnvImage = pkgs.dockerTools.pullImage {
|
63 | 64 | imageName = "ubuntu";
|
64 | 65 | imageDigest = "sha256:7a520eeb6c18bc6d32a21bb7edcf673a7830813c169645d51c949cecb62387d0";
|
65 | 66 | sha256 = "ajZzFSG/q7F5wAXfBOPpYBT+aVy8lqAXtBzkmAe2SeE=";
|
66 | 67 | finalImageName = "ubuntu";
|
67 | 68 | finalImageTag = "lunar";
|
68 | 69 | };
|
69 |
| - # Build the image and modify it to have the "coder" user. |
| 70 | + # This is an intermediate stage that adds sudo with the setuid bit set. |
| 71 | + # Nix doesn't allow setuid binaries in the store, so we have to do this |
| 72 | + # in a separate stage. |
70 | 73 | intermediateDevEnvImage = pkgs.dockerTools.buildImage {
|
71 | 74 | name = "intermediate";
|
72 | 75 | fromImage = baseDevEnvImage;
|
73 |
| - # This replaces the "ubuntu" user with "coder" and |
74 |
| - # gives it sudo privileges! |
75 | 76 | runAsRoot = ''
|
76 | 77 | #!${pkgs.runtimeShell}
|
77 | 78 | ${pkgs.dockerTools.shadowSetup}
|
|
83 | 84 | --uid=1000 \
|
84 | 85 | --user-group \
|
85 | 86 | --groups docker
|
86 |
| - cp ${pkgs.sudo}/bin/sudo /usr/bin/sudo |
87 |
| - chmod 4755 /usr/bin/sudo |
| 87 | + cp ${pkgs.sudo}/bin/sudo usr/bin/sudo |
| 88 | + chmod 4755 usr/bin/sudo |
| 89 | + mkdir -p /etc/init.d |
88 | 90 | '';
|
89 | 91 | };
|
90 |
| - |
91 |
| - devEnvPath = "PATH=${pkgs.lib.makeBinPath devShellPackages}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/coder/go/bin"; |
92 |
| - dockerDebianInit = pkgs.fetchFromGitHub { |
93 |
| - owner = "moby"; |
94 |
| - repo = "moby"; |
95 |
| - rev = "ae737656f9817fbd5afab96aa083754cfb81aab0"; |
96 |
| - sha256 = "sha256-oS3WplsxhKHCuHwL4/ytsCNJ1N/SZhlUZmzZTf81AoE="; |
97 |
| - }; |
| 92 | + # Environment variables that live in `/etc/environment` in the container. |
| 93 | + # These will also be applied to the container config. |
| 94 | + devEnvVars = [ |
| 95 | + "PATH=${pkgs.lib.makeBinPath devShellPackages}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/coder/go/bin" |
| 96 | + #This setting prevents Go from using the public checksum database for |
| 97 | + # our module path prefixes. It is required because these are in private |
| 98 | + # repositories that require authentication. |
| 99 | + # |
| 100 | + # For details, see: https://golang.org/ref/mod#private-modules |
| 101 | + "GOPRIVATE=coder.com,cdr.dev,go.coder.com,github.com/cdr,github.com/coder" |
| 102 | + # Increase memory allocation to NodeJS |
| 103 | + "NODE_OPTIONS=--max_old_space_size=8192" |
| 104 | + ]; |
| 105 | + # Builds a layered image with all the tools included! |
98 | 106 | devEnvImage = pkgs.dockerTools.streamLayeredImage {
|
99 | 107 | name = "codercom/oss-dogfood";
|
100 | 108 | tag = "testing";
|
101 | 109 | fromImage = intermediateDevEnvImage;
|
102 | 110 | maxLayers = 64;
|
103 |
| - extraCommands = '' |
104 |
| - mkdir -p etc |
105 |
| - echo ${devEnvPath} > etc/environment |
106 |
| - |
107 |
| - mkdir -p etc/default |
108 |
| - echo 'DOCKERD=${pkgs.docker}/bin/dockerd' > etc/default/docker |
109 |
| - mkdir -p etc/init.d |
110 |
| - cp ${dockerDebianInit}/contrib/init/sysvinit-debian/docker etc/init.d/docker |
111 |
| - echo "coder ALL=(ALL) NOPASSWD:ALL" >etc/sudoers |
112 |
| - mkdir -p etc/pam.d |
113 |
| - cat > etc/pam.d/other <<EOF |
| 111 | + contents = [ |
| 112 | + # Required for `sudo` to persist the proper `PATH`. |
| 113 | + ( |
| 114 | + pkgs.writeTextDir "etc/environment" (pkgs.lib.strings.concatLines devEnvVars) |
| 115 | + ) |
| 116 | + # Allows `coder` to use `sudo` without a password. |
| 117 | + ( |
| 118 | + pkgs.writeTextDir "etc/sudoers" '' |
| 119 | + coder ALL=(ALL) NOPASSWD:ALL |
| 120 | + '' |
| 121 | + ) |
| 122 | + # Also allows `coder` to use `sudo` without a password. |
| 123 | + ( |
| 124 | + pkgs.writeTextDir "etc/pam.d/other" '' |
114 | 125 | account sufficient pam_unix.so
|
115 | 126 | auth sufficient pam_rootok.so
|
116 | 127 | password requisite pam_unix.so nullok yescrypt
|
117 | 128 | session required pam_unix.so
|
118 |
| - EOF |
119 |
| - mkdir -p etc/ssl/certs |
120 |
| - cp -r ${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt etc/ssl/certs/ca-certificates.crt |
121 |
| - ''; |
| 129 | + '' |
| 130 | + ) |
| 131 | + # This is the debian script for managing Docker with `sudo service docker ...`. |
| 132 | + ( |
| 133 | + pkgs.writeTextFile { |
| 134 | + name = "docker"; |
| 135 | + destination = "/etc/init.d/docker"; |
| 136 | + executable = true; |
| 137 | + text = (builtins.readFile ( |
| 138 | + pkgs.fetchFromGitHub |
| 139 | + { |
| 140 | + owner = "moby"; |
| 141 | + repo = "moby"; |
| 142 | + rev = "ae737656f9817fbd5afab96aa083754cfb81aab0"; |
| 143 | + sha256 = "sha256-oS3WplsxhKHCuHwL4/ytsCNJ1N/SZhlUZmzZTf81AoE="; |
| 144 | + } + "/contrib/init/sysvinit-debian/docker" |
| 145 | + )); |
| 146 | + } |
| 147 | + ) |
| 148 | + # The Docker script above looks here for the daemon binary location. |
| 149 | + # Because we're injecting it with Nix, it's not in the default spot. |
| 150 | + ( |
| 151 | + pkgs.writeTextDir "etc/default/docker" '' |
| 152 | + DOCKERD=${pkgs.docker}/bin/dockerd |
| 153 | + '' |
| 154 | + ) |
| 155 | + # The same as `sudo apt install ca-certificates -y'. |
| 156 | + ( |
| 157 | + pkgs.writeTextDir "etc/ssl/certs/ca-certificates.crt" |
| 158 | + (builtins.readFile "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt") |
| 159 | + ) |
| 160 | + ]; |
122 | 161 |
|
123 | 162 | config = {
|
124 |
| - Env = [ |
125 |
| - devEnvPath |
126 |
| - #This setting prevents Go from using the public checksum database for |
127 |
| - # our module path prefixes. It is required because these are in private |
128 |
| - # repositories that require authentication. |
129 |
| - # |
130 |
| - # For details, see: https://golang.org/ref/mod#private-modules |
131 |
| - "GOPRIVATE=coder.com,cdr.dev,go.coder.com,github.com/cdr,github.com/coder" |
132 |
| - # Increase memory allocation to NodeJS |
133 |
| - "NODE_OPTIONS=--max_old_space_size=8192" |
134 |
| - ]; |
| 163 | + Env = devEnvVars; |
135 | 164 | Entrypoint = [ "/bin/bash" ];
|
136 | 165 | User = "coder";
|
137 | 166 | };
|
138 | 167 | };
|
139 | 168 | in
|
140 | 169 | {
|
141 | 170 | packages = {
|
142 |
| - devEnvironmentDocker = devEnvImage; |
143 |
| - # other packages you want to define for this system |
| 171 | + devEnvImage = devEnvImage; |
144 | 172 | };
|
145 | 173 | defaultPackage = formatter; # or replace it with your desired default package.
|
146 | 174 | devShell = pkgs.mkShell { buildInputs = devShellPackages; };
|
|
0 commit comments