Skip to content

Instantly share code, notes, and snippets.

@sheldonhull
Last active July 17, 2022 00:36
Show Gist options
  • Save sheldonhull/2f59949dae126ee4f49cfafe1f235a01 to your computer and use it in GitHub Desktop.
Save sheldonhull/2f59949dae126ee4f49cfafe1f235a01 to your computer and use it in GitHub Desktop.
[using nix for tooling inside docker] Test case of using nix for replacing custom steps in a dockerfile for dev tooling #nix #codespaces #help

Using Nix CLI

FROM mcr.microsoft.com/vscode/devcontainers/universal:1-${VARIANT} as BASE

RUN sh <(curl -L https://nixos.org/nix/install) --no-daemon
RUN mkdir -p /home/codespace/.config/nixpkgs && echo '{ allowUnfree = true; }' >> /home/codespace/.config/nixpkgs/config.nix
RUN echo '. /home/codespace/.nix-profile/etc/profile.d/nix.sh' >> /home/codespace/.bashrc

# Install git
RUN . /home/codespace/.nix-profile/etc/profile.d/nix.sh \
  && nix-env -i git git-lfs

# This requires dotnet install so bypassing as not critical
# RUN HOMEBREW_NO_AUTO_UPDATE=1 brew install gitversion
# Install direnv
RUN . /home/codespace/.nix-profile/etc/profile.d/nix.sh \
  && nix-env -i direnv \
  && direnv hook bash >> /home/codespace/.bashrc \
  && direnv allow

I'll eventually replace the customization with chezmoi init and ensure nix is loaded in my dotfiles.

My default.nix file is:

{ pkgs, ... }:
{
environment.systemPackages = with pkgs; [
  curl
  git
  htop
  build-essential
  file
  gcc
  tree
  wget
  unzip
  pv
  go
  git-town
  hugo
  chezmoi
  nodejs
  nixfmt
];

# pkgs.mkShell {  <--- how do I configure these as requiring installation outside of my nix-shell?
#   buildInputs = [
#     nixpkgs.go
#     nixpkgs.git-town
#     nixpkgs.hugo
#     nixpkgs.chezmoi
#     nixpkgs.nodejs
#     nixpkgs.nixfmt
#   ];
}

I also setup .envrc

use nix

and then loaded my profile with . /home/codespace/.nix-profile/etc/profile.d/nix.sh. After having run direnv allow, it loaded correctly.

Next Steps

How do I do the equivalent of:

RUN  nix-env -iA nixpkgs.go \
&& nix-env -iA nixpkgs.git-town \
&& nix-env -iA nixpkgs.hugo \
&& nix-env -iA nixpkgs.chezmoi \
&& nix-env -iA nixpkgs.nodejs

into a command that would load this?

RUN nix-env install default.nix

This woudld give me flexibility to run local or in a container with the same packages getting setup and also simplify my Dockerfiles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment