Skip to content

Commit

Permalink
Add a working devcontainer configuration.
Browse files Browse the repository at this point in the history
This config will automatically pick up and install the devShell
dependencies from `flake.nix` when the container is created or started.
If you are editing the flake.nix in the meantime, you'll need to open a
terminal and run `update-content.sh`. You may need to reload the remote
vscode window in order for this to take effect, but I tested that it
works with the golang extension.
  • Loading branch information
peterldowns committed Mar 1, 2023
1 parent 9111cbf commit 1fd9cf2
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 12 deletions.
68 changes: 68 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Latest debian, with curl, should contain all necessary dependencies
# https://github.com/devcontainers/images/tree/main/src/base-debian
# FROM mcr.microsoft.com/devcontainers/base:debian
# https://github.com/docker-library/buildpack-deps/blob/98a5ab81d47a106c458cdf90733df0ee8beea06c/debian/buster/curl/Dockerfile
FROM buildpack-deps:curl

# Update and install necessary system packages
RUN apt-get update \
&& apt-get install -y \
sudo \
xz-utils \
tar \
curl \
procps \
zsh

# Create a non-root user
# from https://code.visualstudio.com/remote/advancedcontainers/add-nonroot-user#_creating-a-nonroot-user
# alternatively, could use this feature to do it automagically at boot time
# https://github.com/devcontainers/features/blob/main/src/common-utils/main.sh#L371
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID
# Create the user with sudo privileges
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME --shell /bin/zsh \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME

# Install nix with flakes and the unified-command enabled
RUN curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix > /nix-installer && chmod +x /nix-installer
RUN /nix-installer install linux --init none --no-confirm

# Add user to trusted users
RUN echo "extra-trusted-users = $USERNAME" | sudo tee -a /etc/nix/nix.conf >/dev/null
# Allow the user to use the nix daemon without sudo
RUN usermod -a -G nixbld $USERNAME

# Initialize nix
COPY nixinstall.sh /tmp/nixinstall.sh
RUN chmod +x /tmp/nixinstall.sh && /tmp/nixinstall.sh && rm /tmp/nixinstall.sh

# Add the entrypoint
COPY entrypoint.sh /tmp/entrypoint.sh
RUN install --mode 755 /tmp/entrypoint.sh /usr/local/bin/entrypoint.sh && rm /tmp/entrypoint.sh

COPY update-content.sh /tmp/update-content.sh
RUN install --mode 755 /tmp/update-content.sh /usr/local/bin/update-content.sh && rm /tmp/update-content.sh

# Make this bash wrapper the default source for all scripts
COPY sh.sh /tmp/sh.sh
RUN rm /bin/sh && install --mode 755 /tmp/sh.sh /bin/sh && rm /tmp/sh.sh

# Enable direnv on all directories by default
USER $USERNAME
RUN mkdir -p /home/$USERNAME/.config/direnv/
COPY direnv.toml /home/$USERNAME/.config/direnv/direnv.toml
# setup direnv and starship bash and zsh
RUN touch ~/.zshrc ~/.bashrc
RUN echo 'eval "$(direnv hook zsh)"' >> /home/$USERNAME/.zshenv
RUN echo 'eval "$(starship init zsh)"' >> /home/$USERNAME/.zshenv
RUN echo 'eval "$(direnv hook bash)"' >> /home/$USERNAME/.bash_aliases
RUN echo 'eval "$(starship init bash)"' >> /home/$USERNAME/.bash_aliases

# By default, starting the container will run forever. This is necessary for
# some reason that I don't understand.
ENTRYPOINT [ "entrypoint.sh" ]
CMD ["sleep", "infinity"]
22 changes: 22 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "default-nix",
"build": {
"dockerfile": "Dockerfile",
},
"overrideCommand": false,
"remoteUser": "vscode",
"updateContentCommand": "update-content.sh nix-search-cli",
"postStartCommand": "update-content.sh nix-search-cli",
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.defaultProfile.linux": "zsh",
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/bin/zsh",
},
},
},
},
},
}
5 changes: 5 additions & 0 deletions .devcontainer/direnv.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[global]
warn_timeout = "10m"

[whitelist]
prefix = [ "/" ]
12 changes: 12 additions & 0 deletions .devcontainer/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env zsh
start_nix_daemon() {
x=$(pgrep nix-daemon)
if [[ -z "$x" ]]; then
echo "started new daemon"
sudo -i --background --non-interactive zsh -c 'nix-daemon >& /tmp/nix-daemon.log'
else
echo "nix-daemon running PID=$x"
fi
}
start_nix_daemon
exec "$@"
21 changes: 21 additions & 0 deletions .devcontainer/nixinstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#/usr/bin/env zsh
# start the nix daemon
start_nix_daemon() {
x=$(pgrep nix-daemon)
if [ -z "$x" ]; then
echo "started new daemon"
sudo -i --background --non-interactive zsh -c 'nix-daemon >& /tmp/nix-daemon.log'
else
echo "nix-daemon running PID=$x"
fi
}

start_nix_daemon
# pin nixpkgs
echo "downloading and pinnng nixpkgs"
sudo -i nix registry add nixpkgs github:NixOS/nixpkgs
sudo -i nix registry pin nixpkgs
echo "installing base set of packages"
# install some packages
sudo -i nix-env -iA nixpkgs.direnv nixpkgs.starship nixpkgs.git
echo "done"
11 changes: 11 additions & 0 deletions .devcontainer/sh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

start_nix_daemon() {
x=$(pgrep nix-daemon)
if [[ -z "$x" ]]; then
sudo -i --background --non-interactive zsh -c 'nix-daemon >& /tmp/nix-daemon.log'
fi
}

start_nix_daemon
BASH_ENV=~/.bash_aliases bash "$@"
5 changes: 5 additions & 0 deletions .devcontainer/update-content.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env zsh
WORKSPACE_ROOT=/workspaces/$1
echo 'export WORKSPACE_ROOT='$WORKSPACE_ROOT > ~/.bash_aliases
cd $WORKSPACE_ROOT
nix print-dev-env . >> ~/.bash_aliases
3 changes: 2 additions & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Load the development shell using Nix, via one of:
#
# - Lorri https://github.com/nix-community/lorri
# - use flake https://direnv.net/man/direnv-stdlib.1.html#codeuse-flake-ltinstallablegtcode
# - Lorri https://github.com/nix-community/lorri
# - use nix https://direnv.net/man/direnv-stdlib.1.html#codeuse-nix-code
#
# in that order of preference
Expand All @@ -18,6 +18,7 @@ if has use_flake; then
exit
fi


if has use_nix; then
echo "direnv: loading env from use_nix"
use nix
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,37 @@
devShells = rec {
default = pkgs.mkShell {
packages = with pkgs; [
# golang
## golang
delve
go-outline
go
golangci-lint
gopkgs
gopls
gotools
# nix
## nix
pkgs.gomod2nix # have to use pkgs. prefix or it breaks lorri
rnix-lsp
#rnix-lsp
nixpkgs-fmt
# other tools
## other tools
just
];

shellHook = ''
# The path to this repository
shell_nix="''${IN_LORRI_SHELL:-$(pwd)/shell.nix}"
workspace_root=$(dirname "$shell_nix")
export WORKSPACE_ROOT="$workspace_root"
if [ -z $WORKSPACE_ROOT ]; then
shell_nix="''${IN_LORRI_SHELL:-$(pwd)/shell.nix}"
workspace_root=$(dirname "$shell_nix")
export WORKSPACE_ROOT="$workspace_root"
fi
# We put the $GOPATH/$GOCACHE/$GOENV in $TOOLCHAIN_ROOT,
# and ensure that the GOPATH's bin dir is on our PATH so tools
# can be installed with `go install`.
#
# Any tools installed explicitly with `go install` will take precedence
# over versions installed by Nix due to the ordering here.
export TOOLCHAIN_ROOT="$workspace_root/.toolchain"
export TOOLCHAIN_ROOT="$WORKSPACE_ROOT/.toolchain"
export GOROOT=
export GOCACHE="$TOOLCHAIN_ROOT/go/cache"
export GOENV="$TOOLCHAIN_ROOT/go/env"
Expand Down

0 comments on commit 1fd9cf2

Please sign in to comment.