NixOS & Nix Shell Container Configurations

Setting Up A Docker Data Container

Used to collect the Nix packages that can be accessed from other containers.

  • Create a Docker data container named nix to use a shared persistent /nix for all your Nix containers.
docker create --name nix -v nix:/nix --network toilethill-net nixos/nix sh
  • To list the volumes in the /nix volume and list all installed nix packages.
docker run --rm --volumes-from nix nixos/nix ls -la /nix
  • Clean up by purging nix-store cache:
docker run --rm --volumes-from=nix nixos/nix nix-collect-garbage -d

Nix Shell with a Custom Nix Configuration

  • To use your own nix configuration within a container:
  • Assumes you have a custom Nix Expression file in a .config sub dir in $(pwd).
docker run --rm --volume=$(pwd):/home --volumes-from=nix -it nixos/nix nix-shell /home/.config/default.nix --run 'echo "Holy Cow! This is awsome!!" | cowsay'
  • GitHub Gist

Haskell Environment:

docker run --rm --volumes-from=nix -it -v $(pwd):/home -w /home -p 3000:3000 nixos/nix nix-shell /home/.config/haskell.nix
  • Github Gist

GitHub Pages with Jekyll

  • Create a GitHub repo for your GitHub Pages site pages.github.com.
  • Run the commands below and clone your repo into a directory blog
  • Copy any files from a starter Jekyll project
    mkdir <your-project>
    cd <your-project>
    git clone git@github.com:<repo-path>.git blog
    
  • Download the Nix scripts in the Gist below into <your-project> directory.
  • GitHub Gist

  • To update a Jekyl site.
    docker run --rm --volumes-from=nix -it -v=$(pwd):/home -w=/home nixos/nix nix-shell /home/update.nix
    
  • To build and serve a local Jekyll site.
    docker run --rm --volumes-from=nix -it -v=$(pwd):/home -w=/home --ip=localhost -p=4000:4000 nixos/nix nix-shell /home/post.nix
    

Commands

  • Install a Nix package from nix-shell
    nix-env -iA nixpkgs.ruby
    

Nix in Docker – Best of Both Worlds