11FROM 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
58RUN 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
1232ENV MAX_JOBS=${MAX_JOBS}
1333ENV 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
1743set -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
4773function 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
7298function 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
182223esac
183224EOF
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" ]
0 commit comments