Skip to content

Commit af10b7c

Browse files
authored
Merge pull request #96 from gadget-inc/devcontainer
Add a nixified devcontainer setup for codespaces
2 parents 753a546 + b6a0a67 commit af10b7c

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

.devcontainer/Dockerfile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.202.1/containers/debian/.devcontainer/base.Dockerfile
2+
3+
# [Choice] Debian version (use bullseye or stretch on local arm64/Apple Silicon): bullseye, buster, stretch
4+
ARG VARIANT="buster"
5+
FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
6+
7+
# use bash to run commands in the dockerfile so we can easily source the nix environment
8+
SHELL ["/bin/bash", "-c"]
9+
10+
# install dependencies nix needs to install itself
11+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
12+
&& apt-get -y install --no-install-recommends xz-utils acl
13+
14+
# set up chsh to allow passwordless shell changing
15+
RUN sed -i 's/required/sufficient/' /etc/pam.d/chsh
16+
17+
# setup the default vscode user that codespaces uses as a passwordless sudoer
18+
RUN usermod -aG sudo vscode && echo "vscode ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers
19+
USER vscode
20+
ENV USER=vscode
21+
22+
# Install nix (requires non-root user)
23+
RUN curl -L https://nixos.org/nix/install | sh -s -- --no-daemon && \
24+
echo 'source ~/.nix-profile/etc/profile.d/nix.sh' >> ~/.bashrc
25+
26+
# Enable required Nix experimental features & pin the nixpkgs flake
27+
ENV NIXPKGS_COMMIT=ec750fd01963ab6b20ee1f0cb488754e8036d89d
28+
RUN source ~/.nix-profile/etc/profile.d/nix.sh && \
29+
nix-env -f https://github.com/NixOS/nixpkgs/archive/$NIXPKGS_COMMIT.tar.gz -iA nix && \
30+
mkdir -p ~/.config/nix && \
31+
echo 'experimental-features = nix-command flakes' >> ~/.config/nix/nix.conf && \
32+
nix registry pin nixpkgs github:NixOS/nixpkgs/$NIXPKGS_COMMIT
33+
34+
# Install & setup direnv
35+
RUN source ~/.nix-profile/etc/profile.d/nix.sh && \
36+
nix profile install nixpkgs#direnv nixpkgs#nix-direnv && \
37+
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc && \
38+
echo 'source ~/.nix-profile/share/nix-direnv/direnvrc' >> ~/.direnvrc
39+
40+
# Install docker and setup buildx as default docker builder
41+
RUN source ~/.nix-profile/etc/profile.d/nix.sh && \
42+
nix profile install nixpkgs#docker && \
43+
docker buildx install

.devcontainer/bootstrap.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
# source the nix environment
4+
source /home/vscode/.nix-profile/etc/profile.d/nix.sh
5+
6+
set -euox pipefail
7+
8+
# fixes nix builds within github codespaces, see https://github.com/xtruder/nix-devcontainer/issues/12
9+
sudo setfacl --remove-default /tmp
10+
11+
sudo locale-gen en_US.UTF-8
12+
13+
# Pre-load development environment
14+
direnv allow
15+
nix print-dev-env > /dev/null

.devcontainer/devcontainer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2+
// https://github.com/microsoft/vscode-dev-containers/tree/v0.202.1/containers/debian
3+
{
4+
"name": "Debian",
5+
"runArgs": ["--init"],
6+
"build": {
7+
"dockerfile": "Dockerfile",
8+
// Update 'VARIANT' to pick an Debian version: bullseye, buster, stretch
9+
// Use bullseye or stretch on local arm64/Apple Silicon.
10+
"args": { "VARIANT": "bullseye" }
11+
},
12+
13+
// Set *default* container specific settings.json values on container create.
14+
"settings": {},
15+
16+
// Add the IDs of extensions you want installed when the container is created.
17+
"extensions": [],
18+
19+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
20+
"forwardPorts": [
21+
"3000", // the app
22+
"9000", // webpack-dev-server
23+
"5432" // postgres
24+
],
25+
26+
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
27+
"mounts": ["source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind"],
28+
29+
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
30+
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
31+
32+
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
33+
"remoteUser": "vscode",
34+
35+
// setup nix which we need the source tree for, which only is available after building the devcontainer
36+
"postCreateCommand": ".devcontainer/bootstrap.sh"
37+
}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ yarn.lock
55
vscode-profile*
66
isolate-*
77
flamegraph.html
8-
*.perf
8+
*.perf
9+
.vscode/settings.json

0 commit comments

Comments
 (0)