Skip to content

Commit 215a9d1

Browse files
authored
chore: experiment building dogfood image with nix (#11680)
1 parent 13cbca6 commit 215a9d1

File tree

8 files changed

+223
-26
lines changed

8 files changed

+223
-26
lines changed

.dockerignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Ignore all files and folders
2+
**
3+
4+
# Include flake.nix and flake.lock
5+
!flake.nix
6+
!flake.lock

.github/workflows/dogfood.yaml

+19-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ on:
77
paths:
88
- "dogfood/**"
99
- ".github/workflows/dogfood.yaml"
10+
- "flake.lock"
11+
- "flake.nix"
1012
pull_request:
1113
paths:
1214
- "dogfood/**"
1315
- ".github/workflows/dogfood.yaml"
16+
- "flake.lock"
17+
- "flake.nix"
1418
workflow_dispatch:
1519

1620
jobs:
@@ -45,17 +49,31 @@ jobs:
4549
username: ${{ secrets.DOCKERHUB_USERNAME }}
4650
password: ${{ secrets.DOCKERHUB_PASSWORD }}
4751

48-
- name: Build and push
52+
- name: Build and push Non-Nix image
4953
uses: depot/build-push-action@v1
5054
with:
5155
project: b4q6ltmpzh
5256
token: ${{ secrets.DEPOT_TOKEN }}
5357
buildx-fallback: true
5458
context: "{{defaultContext}}:dogfood"
5559
pull: true
60+
save: true
5661
push: ${{ github.ref == 'refs/heads/main' }}
5762
tags: "codercom/oss-dogfood:${{ steps.docker-tag-name.outputs.tag }},codercom/oss-dogfood:latest"
5863

64+
- name: Build and push Nix image
65+
uses: depot/build-push-action@v1
66+
with:
67+
project: b4q6ltmpzh
68+
token: ${{ secrets.DEPOT_TOKEN }}
69+
buildx-fallback: true
70+
context: "."
71+
file: "dogfood/Dockerfile.nix"
72+
pull: true
73+
save: true
74+
push: ${{ github.ref == 'refs/heads/main' }}
75+
tags: "codercom/oss-dogfood-nix:${{ steps.docker-tag-name.outputs.tag }},codercom/oss-dogfood-nix:latest"
76+
5977
deploy_template:
6078
needs: build_image
6179
runs-on: ubuntu-latest

dogfood/Dockerfile.nix

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Build stage
2+
FROM nixos/nix:2.19.2 as nix
3+
4+
# enable --experimental-features 'nix-command flakes' globally
5+
# nix does not enable these features by default these are required to run commands like
6+
# nix develop -c 'some command' or to use falke.nix
7+
RUN mkdir -p /etc/nix && \
8+
echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf
9+
10+
# Copy Nix flake and install dependencies
11+
COPY flake.* /app/
12+
RUN nix profile install "/app#all" --priority 4 && \
13+
rm -rf /app && \
14+
nix-collect-garbage -d
15+
16+
# Final image
17+
FROM codercom/enterprise-base:latest as final
18+
19+
# Set the non-root user
20+
USER root
21+
22+
# Copy the Nix related files into the Docker image
23+
COPY --from=nix --chown=coder:coder /nix /nix
24+
COPY --from=nix /etc/nix /etc/nix
25+
COPY --from=nix --chown=coder:coder /root/.nix-profile /home/coder/.nix-profile
26+
COPY --from=nix /etc/passwd /etc/passwd.nix
27+
COPY --from=nix /etc/group /etc/group.nix
28+
29+
# Merge the passwd and group files
30+
# We need all nix users and groups to be available in the final image
31+
RUN cat /etc/passwd.nix >> /etc/passwd && \
32+
cat /etc/group.nix >> /etc/group && \
33+
rm /etc/passwd.nix /etc/group.nix
34+
35+
# Set environment variables and PATH
36+
ENV PATH=/home/coder/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:$PATH \
37+
GOPRIVATE="coder.com,cdr.dev,go.coder.com,github.com/cdr,github.com/coder" \
38+
NODE_OPTIONS="--max-old-space-size=8192"
39+
40+
# Set the user to 'coder'
41+
USER coder
42+
WORKDIR /home/coder

dogfood/main.tf

+23-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ locals {
3434
repo_base_dir = replace(data.coder_parameter.repo_base_dir.value, "/^~\\//", "/home/coder/")
3535
repo_dir = module.git-clone.repo_dir
3636
container_name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
37-
registry_name = "codercom/oss-dogfood"
3837
jfrog_host = replace(var.jfrog_url, "https://", "")
3938
}
4039

@@ -46,6 +45,23 @@ data "coder_parameter" "repo_base_dir" {
4645
mutable = true
4746
}
4847

48+
data "coder_parameter" "image_type" {
49+
type = "string"
50+
name = "Coder Image"
51+
default = "codercom/oss-dogfood:latest"
52+
description = "The Docker image used to run your workspace. Choose between nix and non-nix images."
53+
option {
54+
icon = "/icon/coder.svg"
55+
name = "Non-Nix"
56+
value = "codercom/oss-dogfood:latest"
57+
}
58+
option {
59+
icon = "/icons/nix.svg"
60+
name = "Nix"
61+
value = "codercom/oss-dogfood-nix:latest"
62+
}
63+
}
64+
4965
data "coder_parameter" "region" {
5066
type = "string"
5167
name = "Region"
@@ -248,6 +264,9 @@ resource "coder_agent" "dev" {
248264
set -eux -o pipefail
249265
# Start Docker service
250266
sudo service docker start
267+
# Install playwright dependencies
268+
# We want to use the playwright version from site/package.json
269+
cd "${local.repo_dir}/site" && pnpm install && pnpm playwright:install
251270
EOT
252271
}
253272

@@ -279,15 +298,16 @@ resource "docker_volume" "home_volume" {
279298
}
280299

281300
data "docker_registry_image" "dogfood" {
282-
name = "${local.registry_name}:latest"
301+
name = data.coder_parameter.image_type.value
283302
}
284303

285304
resource "docker_image" "dogfood" {
286-
name = "${local.registry_name}@${data.docker_registry_image.dogfood.sha256_digest}"
305+
name = "${data.coder_parameter.image_type.value}@${data.docker_registry_image.dogfood.sha256_digest}"
287306
pull_triggers = [
288307
data.docker_registry_image.dogfood.sha256_digest,
289308
sha1(join("", [for f in fileset(path.module, "files/*") : filesha1(f)])),
290309
filesha1("Dockerfile"),
310+
filesha1("Dockerfile.nix"),
291311
]
292312
keep_locally = true
293313
}

flake.lock

+35-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
inputs = {
55
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
66
flake-utils.url = "github:numtide/flake-utils";
7-
drpc.url = "github:storj/drpc/v0.0.32";
7+
drpc.url = "github:storj/drpc/v0.0.33";
88
};
99

1010
outputs = { self, nixpkgs, flake-utils, drpc }:
@@ -45,14 +45,11 @@
4545
kubernetes-helm
4646
less
4747
# Needed for many LD system libs!
48-
libuuid
48+
util-linux
4949
mockgen
5050
nfpm
5151
nodejs
5252
nodejs.pkgs.pnpm
53-
nodejs.pkgs.prettier
54-
nodejs.pkgs.typescript
55-
nodejs.pkgs.typescript-language-server
5653
openssh
5754
openssl
5855
pango
@@ -78,10 +75,16 @@
7875
zsh
7976
zstd
8077
];
78+
79+
allPackages = pkgs.buildEnv {
80+
name = "all-packages";
81+
paths = devShellPackages;
82+
};
8183
in
8284
{
8385
defaultPackage = formatter; # or replace it with your desired default package.
8486
devShell = pkgs.mkShell { buildInputs = devShellPackages; };
87+
packages.all = allPackages;
8588
}
8689
);
8790
}

site/src/theme/icons.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"matlab.svg",
5555
"memory.svg",
5656
"microsoft.svg",
57+
"nix.svg",
5758
"node.svg",
5859
"nodejs.svg",
5960
"nomad.svg",

0 commit comments

Comments
 (0)