Skip to content
This repository was archived by the owner on Apr 6, 2026. It is now read-only.

Commit 1bd96b7

Browse files
authored
feat: update docker for remote build and push (#115)
This PR enables the container to run the nix build command as a non root user. This functionality is required to allow the builder to run on hfjobs; which sets the user is 1000. deploy a build job via ```bash hfjobs run \ drbh/kernel-builder-dev:job-runner-0 \ /home/nixuser/bin/cli.sh \ fetch \ https://huggingface.co/kernels-community/activation.git ``` notes - `drbh/kernel-builder-dev:job-runner-0` is an image of the attached Dockerfile - `/home/nixuser/bin/cli.sh` is the entrypoint that needs to be specified on `hfjobs` - `fetch` is the docker command - `https://huggingface.co/kernels-community/activation.git` is the kernel repo to build currently this feature does not push the built files to the hub but that can/should be added in a followup PR when a suitable upload strategy is defined.
1 parent fb5ecc9 commit 1bd96b7

3 files changed

Lines changed: 87 additions & 39 deletions

File tree

Dockerfile

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,44 @@
11
FROM nixos/nix:2.18.8
2+
23
# default build args
3-
ARG MAX_JOBS=4
4-
ARG CORES=4
4+
ARG MAX_JOBS=1
5+
ARG CORES=1
6+
7+
# Set up Nix configuration and user
58
RUN echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf \
69
&& echo "max-jobs = $MAX_JOBS" >> /etc/nix/nix.conf \
710
&& echo "cores = $CORES" >> /etc/nix/nix.conf \
8-
&& nix profile install nixpkgs#cachix nixpkgs#git-lfs \
9-
&& cachix use kernel-builder
10-
WORKDIR /kernelcode
11-
COPY . /etc/kernel-builder/
11+
&& echo "trusted-users = root nixuser" >> /etc/nix/nix.conf \
12+
# Create user entries directly in password and group files
13+
&& echo "nixuser:x:1000:1000:NixOS User:/home/nixuser:/bin/bash" >> /etc/passwd \
14+
&& echo "nixuser:x:1000:" >> /etc/group \
15+
&& mkdir -p /home/nixuser/kernelcode \
16+
# Create Nix directories with proper permissions
17+
&& mkdir -p /nix/var/nix/profiles/per-user/nixuser \
18+
&& mkdir -p /nix/var/nix/gcroots/per-user/nixuser \
19+
&& chown -R 1000:1000 /home/nixuser /nix/var/nix/profiles/per-user/nixuser /nix/var/nix/gcroots/per-user/nixuser \
20+
# Install necessary packages
21+
&& nix profile install nixpkgs#cachix nixpkgs#git-lfs nixpkgs#gawk \
22+
&& cachix use kernel-builder
23+
24+
# Set permissions for Nix directories
25+
RUN chown -R nixuser:nixuser /nix
26+
27+
# Set working directory and copy files
28+
WORKDIR /home/nixuser/kernelcode
29+
COPY --chown=nixuser:nixuser . /home/nixuser/kernel-builder/
30+
31+
# Set environment variables
1232
ENV MAX_JOBS=${MAX_JOBS}
1333
ENV CORES=${CORES}
14-
RUN mkdir -p /etc/kernelcode && \
15-
cat <<'EOF' > /etc/kernelcode/cli.sh
34+
ENV HF_TOKEN=${HF_TOKEN}
35+
ENV HOME=/home/nixuser
36+
ENV PUSH_REVISION=hfjob-build
37+
ENV REPO=kernels-community/job-build-test-repo
38+
39+
# Set up CLI script in nixuser's home
40+
RUN mkdir -p /home/nixuser/bin && \
41+
cat <<'EOF' > /home/nixuser/bin/cli.sh
1642
#!/bin/sh
1743
set -e
1844

@@ -38,23 +64,23 @@ function show_usage {
3864
echo " --cores, -c NUMBER Set number of cores per job (default: $CORES)"
3965
echo ""
4066
echo "Examples:"
41-
echo " docker run -v \$(pwd):/kernelcode kernel-builder:dev build"
42-
echo " docker run -it -v \$(pwd):/kernelcode kernel-builder:dev dev"
67+
echo " docker run -v \$(pwd):/home/nixuser/kernelcode kernel-builder:dev build"
68+
echo " docker run -it -v \$(pwd):/home/nixuser/kernelcode kernel-builder:dev dev"
4369
echo " docker run kernel-builder:dev fetch https://huggingface.co/user/repo.git"
4470
}
4571

4672
# Function to generate a basic flake.nix if it doesn't exist
4773
function ensure_flake_exists {
48-
if [ ! -f "/kernelcode/flake.nix" ]; then
74+
if [ ! -f "/home/nixuser/kernelcode/flake.nix" ]; then
4975
echo "No flake.nix found, creating a basic one..."
50-
cat <<'FLAKE_EOF' > /kernelcode/flake.nix
76+
cat <<'FLAKE_EOF' > /home/nixuser/kernelcode/flake.nix
5177
{
5278
description = "Flake for Torch kernel extension";
5379

5480
inputs = {
5581
kernel-builder.url = "github:huggingface/kernel-builder";
5682
};
57-
83+
5884
outputs = { self, kernel-builder, }:
5985
kernel-builder.lib.genFlakeOutputs {
6086
path = ./.;
@@ -72,9 +98,9 @@ FLAKE_EOF
7298
function build_extension {
7399
echo "Building Torch Extension Bundle"
74100
# Check if kernelcode is a git repo and get hash if possible
75-
if [ -d "/kernelcode/.git" ]; then
101+
if [ -d "/home/nixuser/kernelcode/.git" ]; then
76102
# Mark git as safe to allow commands
77-
git config --global --add safe.directory /kernelcode
103+
git config --global --add safe.directory /home/nixuser/kernelcode
78104
# Try to get git revision
79105
REV=$(git rev-parse --short=8 HEAD)
80106

@@ -92,17 +118,18 @@ function build_extension {
92118
ensure_flake_exists
93119

94120
# Pure bundle build
121+
# TODO: remove the "bundle" after resolving
95122
echo "Building with Nix..."
96123
nix build \
97-
. \
124+
.\#bundle \
98125
--max-jobs $MAX_JOBS \
99126
-j $CORES \
100-
-L
101-
102-
echo "Build completed. Copying results to /kernelcode/build/"
103-
mkdir -p /kernelcode/build
104-
cp -r --dereference ./result/* /kernelcode/build/
105-
chmod -R u+w /kernelcode/build
127+
-L 2>&1 | awk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0; fflush(); }'
128+
129+
echo "Build completed. Copying results to /home/nixuser/kernelcode/build/"
130+
mkdir -p /home/nixuser/kernelcode/build
131+
cp -r --dereference ./result/* /home/nixuser/kernelcode/build/
132+
chmod -R u+w /home/nixuser/kernelcode/build
106133
echo 'Done'
107134
}
108135

@@ -111,7 +138,7 @@ function start_dev_shell {
111138
echo "Starting development shell..."
112139
# Check for flake.nix or create one
113140
ensure_flake_exists
114-
/root/.nix-profile/bin/nix develop
141+
nix develop
115142
}
116143

117144
# Function to fetch and build from URL
@@ -123,11 +150,25 @@ function fetch_and_build {
123150
fi
124151

125152
echo "Fetching code from $1"
126-
rm -rf /kernelcode/* /kernelcode/.* 2>/dev/null || true
153+
rm -rf /home/nixuser/kernelcode/* /home/nixuser/kernelcode/.* 2>/dev/null || true
127154
git lfs install
128-
git clone "$1" /kernelcode
129-
cd /kernelcode
155+
git clone "$1" /home/nixuser/kernelcode
156+
cd /home/nixuser/kernelcode
130157
build_extension
158+
echo "Build completed. Results are in /home/nixuser/kernelcode/build/"
159+
160+
# skip login to huggingface since token is set in the env
161+
# check user
162+
nix shell nixpkgs#python3 nixpkgs#python3Packages.huggingface-hub -c huggingface-cli whoami
163+
164+
# upload the build to the repo
165+
nix shell nixpkgs#python3 nixpkgs#python3Packages.huggingface-hub -c huggingface-cli \
166+
upload \
167+
--revision ${PUSH_REVISION} \
168+
--commit-message "Build from kernel-builder job" \
169+
${REPO} \
170+
/home/nixuser/kernelcode/build/ \
171+
build/
131172
}
132173

133174
# Parse arguments
@@ -182,5 +223,12 @@ case $COMMAND in
182223
esac
183224
EOF
184225

185-
RUN chmod +x /etc/kernelcode/cli.sh
186-
ENTRYPOINT ["/etc/kernelcode/cli.sh"]
226+
# Set permissions and make the script executable
227+
RUN chmod +x /home/nixuser/bin/cli.sh && \
228+
chown -R nixuser:nixuser /home/nixuser
229+
230+
# Switch to nixuser
231+
USER nixuser
232+
233+
# Use the cli.sh script directly
234+
ENTRYPOINT ["/home/nixuser/bin/cli.sh"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cd examples/activation
3232

3333
# then run the following command to build the kernel
3434
docker run --rm \
35-
-v $(pwd):/kernelcode \
35+
-v $(pwd):/home/nixuser/kernelcode \
3636
ghcr.io/huggingface/kernel-builder:latest
3737
```
3838

docs/docker.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ cd examples/activation
2727

2828
# then run the following command to build the kernel
2929
docker run --rm \
30-
-v $(pwd):/kernelcode \
30+
-v $(pwd):/home/nixuser/kernelcode \
3131
ghcr.io/huggingface/kernel-builder:latest
3232
```
3333

@@ -49,10 +49,10 @@ The kernel builder now includes a command-line interface for easier interaction.
4949

5050
```bash
5151
# Build the kernel (same as the Quick Start example)
52-
docker run --rm -v $(pwd):/kernelcode ghcr.io/huggingface/kernel-builder:latest build
52+
docker run --rm -v $(pwd):/home/nixuser/kernelcode ghcr.io/huggingface/kernel-builder:latest build
5353

5454
# Start an ephemeral development shell
55-
docker run --rm -it -v $(pwd):/kernelcode ghcr.io/huggingface/kernel-builder:latest dev
55+
docker run --rm -it -v $(pwd):/home/nixuser/kernelcode ghcr.io/huggingface/kernel-builder:latest dev
5656

5757
# Build from a Git URL
5858
docker run --rm ghcr.io/huggingface/kernel-builder:latest fetch https://huggingface.co/kernels-community/activation.git
@@ -74,7 +74,7 @@ The kernel builder can be configured in two ways:
7474

7575
```bash
7676
docker run --rm \
77-
-v $(pwd):/kernelcode \
77+
-v $(pwd):/home/nixuser/kernelcode \
7878
-e MAX_JOBS=8 \
7979
-e CORES=8 \
8080
ghcr.io/huggingface/kernel-builder:latest
@@ -91,7 +91,7 @@ You can also specify these parameters using command-line options:
9191

9292
```bash
9393
docker run --rm \
94-
-v $(pwd):/kernelcode \
94+
-v $(pwd):/home/nixuser/kernelcode \
9595
ghcr.io/huggingface/kernel-builder:latest build --jobs 8 --cores 4
9696
```
9797

@@ -102,7 +102,7 @@ For development purposes, you can start an interactive shell with:
102102
```bash
103103
docker run -it \
104104
--name my-dev-env \
105-
-v "$(pwd)":/kernelcode \
105+
-v "$(pwd)":/home/nixuser/kernelcode \
106106
ghcr.io/huggingface/kernel-builder:latest dev
107107
```
108108

@@ -116,7 +116,7 @@ For iterative development, you can create a persistent container to maintain the
116116
# Create a persistent container and start a development shell
117117
docker run -it \
118118
--name my-persistent-dev-env \
119-
-v "$(pwd)":/kernelcode \
119+
-v "$(pwd)":/home/nixuser/kernelcode \
120120
ghcr.io/huggingface/kernel-builder:latest dev
121121
```
122122

@@ -173,7 +173,7 @@ git clone git@hf.co:kernels-community/activation
173173
cd activation
174174
# then run the build command
175175
docker run --rm \
176-
-v $(pwd):/kernelcode \
176+
-v $(pwd):/home/nixuser/kernelcode \
177177
ghcr.io/huggingface/kernel-builder:latest
178178
# we should now have the built kernels on our host
179179
ls result
@@ -195,7 +195,7 @@ This will clone the repository into the container, build the kernels, and save t
195195

196196
```bash
197197
docker run --rm \
198-
-v /path/to/output:/kernelcode/build \
198+
-v /path/to/output:/home/nixuser/kernelcode/build \
199199
ghcr.io/huggingface/kernel-builder:latest fetch https://huggingface.co/kernels-community/activation.git
200200
```
201201

@@ -209,7 +209,7 @@ docker build -t ghcr.io/huggingface/kernel-builder:latest .
209209

210210
# You can build a kernel using this development container:
211211
cd examples/activation
212-
docker run --rm -v $(pwd):/kernelcode ghcr.io/huggingface/kernel-builder:latest
212+
docker run --rm -v $(pwd):/home/nixuser/kernelcode ghcr.io/huggingface/kernel-builder:latest
213213

214214
# copying path '/nix/store/1b79df96k9npmrdgwcljfh3v36f7vazb-source' from 'https://cache.nixos.org'...
215215
# trace: evaluation warning: CUDA versions older than 12.0 will be removed in Nixpkgs 25.05; see the 24.11 release notes for more information

0 commit comments

Comments
 (0)