|
| 1 | +# NixOS Devcontainer Troubleshooting Guide |
| 2 | + |
| 3 | +## Rule 1: Final Test Procedure |
| 4 | + |
| 5 | +**All final tests MUST use devcontainer CLI and observe until fully loaded.** |
| 6 | + |
| 7 | +``` |
| 8 | +Goal Condition: FHS is created and observable |
| 9 | +- /etc/passwd exists |
| 10 | +- /etc/group exists |
| 11 | +- /bin/sh exists |
| 12 | +- systemd is running |
| 13 | +``` |
| 14 | + |
| 15 | +### Test Procedure |
| 16 | + |
| 17 | +```bash |
| 18 | +# 1. Clean up any existing containers |
| 19 | +docker rm -f nixos-local-devcontainer-1 2>/dev/null |
| 20 | + |
| 21 | +# 2. Run devcontainer CLI |
| 22 | +nix-shell -p devcontainer --run "devcontainer up --workspace-folder . --config .devcontainer/nixos-local/devcontainer.json" |
| 23 | + |
| 24 | +# 3. OBSERVE until container is fully loaded: |
| 25 | +# - Wait for "Dev Containers: Finished" or similar success message |
| 26 | +# - Check systemd: docker exec nixos-local-devcontainer-1 systemctl is-system-running |
| 27 | +# - Check FHS: docker exec nixos-local-devcontainer-1 ls -la /etc/passwd /bin/sh |
| 28 | + |
| 29 | +# 4. Goal condition check: |
| 30 | +docker exec nixos-local-devcontainer-1 test -f /etc/passwd && echo "✓ /etc/passwd exists" |
| 31 | +docker exec nixos-local-devcontainer-1 test -f /etc/group && echo "✓ /etc/group exists" |
| 32 | +docker exec nixos-local-devcontainer-1 test -f /bin/sh && echo "✓ /bin/sh exists" |
| 33 | +docker exec nixos-local-devcontainer-1 systemctl is-system-running | grep -q running && echo "✓ systemd running" |
| 34 | +``` |
| 35 | + |
| 36 | +## Rule 2: Proof of Concept First |
| 37 | + |
| 38 | +**When troubleshooting, do NOT wait for GitHub Actions rebuild.** |
| 39 | + |
| 40 | +1. Use existing GHCR image as base |
| 41 | +2. Add modifications in local Dockerfile |
| 42 | +3. Test locally with devcontainer CLI first |
| 43 | +4. Only after POC works, add to container-layeredImage.nix for permanent fix |
| 44 | + |
| 45 | +### Workflow |
| 46 | + |
| 47 | +``` |
| 48 | +GitHub Actions (permanent fix) --------> GHCR Image |
| 49 | + ↑ ↑ |
| 50 | + | | |
| 51 | + | (after POC works) | (for testing) |
| 52 | + | | |
| 53 | + +-------- container-layeredImage <--+ |
| 54 | + ↑ |
| 55 | + | |
| 56 | + (add fix here) |
| 57 | + | |
| 58 | + docker-compose.yml |
| 59 | + + Dockerfile (POC) |
| 60 | + ↓ |
| 61 | + devcontainer CLI |
| 62 | +``` |
| 63 | + |
| 64 | +## Rule 3: Never Copy Binaries - Use Symlinks from Nix Store |
| 65 | + |
| 66 | +**The base image already has all binaries in the nix store. Never copy binaries - create symlinks instead.** |
| 67 | + |
| 68 | +### Why: |
| 69 | +- Copying binaries bloats the image |
| 70 | +- Nix store has all needed binaries (bash, coreutils, etc.) |
| 71 | +- Just create symlinks to the nix store paths |
| 72 | + |
| 73 | +### Finding Nix Store Paths |
| 74 | + |
| 75 | +```bash |
| 76 | +# Find bash in nix store |
| 77 | +docker run --rm ghcr.io/lucernae/devcontainer-nix:nixos--nixos-25.11---x86_64-linux \ |
| 78 | + find /nix/store -name bash -type f 2>/dev/null | head -5 |
| 79 | + |
| 80 | +# Or export and check |
| 81 | +docker create --name temp <image> |
| 82 | +docker export temp | tar -tf - | grep "bin/bash" |
| 83 | +docker rm temp |
| 84 | +``` |
| 85 | + |
| 86 | +## Rule 4: Original NixOS Systemd Must Be PID 1 |
| 87 | + |
| 88 | +**When overriding Docker's entrypoint, the original NixOS systemd entrypoint MUST be PID 1.** |
| 89 | + |
| 90 | +### Why: |
| 91 | +- NixOS expects systemd as PID 1 |
| 92 | +- Systemd handles initialization, services, cgroups |
| 93 | +- Blocking the init process breaks the system |
| 94 | + |
| 95 | +### Correct Pattern (Fork and Wait): |
| 96 | + |
| 97 | +```yaml |
| 98 | +# WRONG - blocks systemd |
| 99 | +entrypoint: ["bash", "-c", "wait_for_ready && /usr/sbin/init"] |
| 100 | + |
| 101 | +# CORRECT - start systemd in background, wait for it |
| 102 | +entrypoint: [ |
| 103 | + "bash", "-c", |
| 104 | + "/usr/sbin/init & # start systemd as PID 1 |
| 105 | + PID1=$! |
| 106 | + wait_for_vscode_ready |
| 107 | + wait $PID1" |
| 108 | +] |
| 109 | +``` |
| 110 | + |
| 111 | +### Alternative: Use postCreateCommand |
| 112 | + |
| 113 | +Instead of blocking entrypoint, use devcontainer's `postCreateCommand`: |
| 114 | + |
| 115 | +```json |
| 116 | +{ |
| 117 | + "postCreateCommand": "bash -c 'systemctl is-system-running --wait && echo Ready'" |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +This runs AFTER container is created and systemd is already running. |
| 122 | + |
| 123 | +## Problem: VS Code Dev Containers fails to connect |
| 124 | + |
| 125 | +### Error: `unable to find user root: no matching entries in passwd file` |
| 126 | + |
| 127 | +**Root Cause:** |
| 128 | +- VS Code runs `uname -m` as a probe to check system architecture |
| 129 | +- This requires a valid `/etc/passwd` entry for root |
| 130 | +- The NixOS activation hasn't run yet when VS Code tries to connect (systemd hasn't fully started) |
| 131 | + |
| 132 | +### Why this happens: |
| 133 | +1. Docker starts container with `/usr/sbin/init` (NixOS init) |
| 134 | +2. NixOS init runs activation scripts (creates `/etc/passwd`, `/etc/group`, sets up `/bin/sh`) |
| 135 | +3. After activation, systemd starts as PID 1 |
| 136 | +4. **BUT** VS Code tries to connect BEFORE systemd is ready |
| 137 | +5. VS Code needs `/etc/passwd` to run its probe commands |
| 138 | + |
| 139 | +## Solutions |
| 140 | + |
| 141 | +### Solution 1: Add placeholder /etc/passwd in image (Permanent Fix) |
| 142 | + |
| 143 | +The VS Code probe runs BEFORE any devcontainer commands (even `onCreateCommand`). There is no built-in way to delay the probe. |
| 144 | + |
| 145 | +**Therefore, the solution is to add placeholder /etc/passwd and /etc/group to the image at build time.** |
| 146 | + |
| 147 | +Add passwd/group files in the NixOS image build (in container-layeredImage.nix): |
| 148 | + |
| 149 | +```nix |
| 150 | +fhsEtc = pkgs.runCommand "fhs-etc" { } '' |
| 151 | + mkdir -p $out/etc |
| 152 | + echo "root:x:0:0:root:/root:/run/current-system/sw/bin/zsh" > $out/etc/passwd |
| 153 | + echo "root:x:0:" > $out/etc/group |
| 154 | + echo "vscode:x:1000:" >> $out/etc/group |
| 155 | +''; |
| 156 | +``` |
| 157 | + |
| 158 | +Include in `contents` array of streamLayeredImage. |
| 159 | + |
| 160 | +### Solution 2: Start container first, then use devcontainer CLI (Workaround) |
| 161 | + |
| 162 | +This is a practical workaround - start the container first, then use devcontainer CLI: |
| 163 | + |
| 164 | +```bash |
| 165 | +# 1. Start container with docker-compose |
| 166 | +docker compose up -d |
| 167 | + |
| 168 | +# 2. Wait for systemd to boot |
| 169 | +sleep 10 |
| 170 | + |
| 171 | +# 3. Now run devcontainer CLI - it will attach to running container |
| 172 | +devcontainer up --workspace-folder . --config .devcontainer/nixos-local/devcontainer.json |
| 173 | +``` |
| 174 | + |
| 175 | +This works because: |
| 176 | +1. docker-compose starts the container and waits for systemd to boot |
| 177 | +2. When systemd is ready, /etc/passwd and /etc/group exist |
| 178 | +3. devcontainer CLI then connects to the already-running container |
| 179 | +4. VS Code probe succeeds because FHS files exist |
| 180 | + |
| 181 | +### Solution 3: Use entrypoint wrapper (Alternative) |
| 182 | + |
| 183 | +Use a custom entrypoint that waits for systemd (Rule 4): |
| 184 | + |
| 185 | +```yaml |
| 186 | +services: |
| 187 | + devcontainer: |
| 188 | + entrypoint: [ |
| 189 | + "bash", "-c", |
| 190 | + "/usr/sbin/init & # start systemd as PID 1 |
| 191 | + PID1=$! |
| 192 | + # wait for systemd to be ready |
| 193 | + for i in 1 2 3 4 5 6 7 8 9 10; do |
| 194 | + sleep 2 |
| 195 | + if systemctl is-system-running 2>/dev/null | grep -q running; then |
| 196 | + break |
| 197 | + fi |
| 198 | + done |
| 199 | + wait $PID1" |
| 200 | + ] |
| 201 | +``` |
| 202 | + |
| 203 | +### Solution 2: Add placeholder /etc/passwd to image (Permanent Fix) |
| 204 | + |
| 205 | +Add passwd/group files in the NixOS image build (in container-layeredImage.nix): |
| 206 | + |
| 207 | +```nix |
| 208 | +fhsEtc = pkgs.runCommand "fhs-etc" { } '' |
| 209 | + mkdir -p $out/etc |
| 210 | + echo "root:x:0:0:root:/root:/run/current-system/sw/bin/zsh" > $out/etc/passwd |
| 211 | + echo "root:x:0:" > $out/etc/group |
| 212 | + echo "vscode:x:1000:" >> $out/etc/group |
| 213 | +''; |
| 214 | +``` |
| 215 | + |
| 216 | +Include in `contents` array of streamLayeredImage. |
| 217 | + |
| 218 | +**IMPORTANT: DO NOT COPY /etc/ in Dockerfile!** |
| 219 | +- Copying /etc/ files to the image will break NixOS activation |
| 220 | +- The /etc/ must be generated by NixOS activation at runtime |
| 221 | +- Placeholder files must be added at Nix expression level (container-layeredImage.nix) |
| 222 | + |
| 223 | +## Current Status |
| 224 | + |
| 225 | +- ✅ systemd boots correctly with `--cgroupns=host` |
| 226 | +- ✅ Nix and NixOS work inside the container |
| 227 | +- ✅ docker-compose works directly with original GHCR image |
| 228 | +- ❌ VS Code Dev Containers fails (needs passwd before systemd ready) |
| 229 | + |
| 230 | +## Debugging |
| 231 | + |
| 232 | +```bash |
| 233 | +# Check systemd status (must show "running") |
| 234 | +docker exec nixos-local-devcontainer-1 systemctl is-system-running |
| 235 | + |
| 236 | +# Check activation logs |
| 237 | +docker logs nixos-local-devcontainer-1 |
| 238 | + |
| 239 | +# Check FHS files exist |
| 240 | +docker exec nixos-local-devcontainer-1 ls -la /etc/passwd |
| 241 | +docker exec nixos-local-devcontainer-1 ls -la /etc/group |
| 242 | +docker exec nixos-local-devcontainer-1 ls -la /bin/sh |
| 243 | + |
| 244 | +# Check Nix works |
| 245 | +docker exec nixos-local-devcontainer-1 nix --version |
| 246 | +docker exec nixos-local-devcontainer-1 nixos-version |
| 247 | +``` |
0 commit comments