About using Nix in my development workflow

asdf

erlang 21.0
elixir 1.7.3-otp-21
  • after installing asdf, you need to install all the needed plugins. It is only a matter of asdf plugin-install <plugin-name> commands, but it is not automated;
  • some languages require dependencies to build. Erlang, for instance, needs some packages to be installed before asdf install erlang <version> works. You need a compiler, ncurses, OpenSSL and quite a few more. Setup instructions depends on the system you use;
  • If your project depends on other tools like, say, fwup for Nerves projects, you still have to provide manual setup instructions.

Nix

/nix/store/k13mm9jqxm2ndlwzsj7zicsq7lpmmjlg-elixir-1.7.3
/nix/store/k13mm9jqxm2ndlwzsj7zicsq7lpmmjlg-elixir-1.7.3/bin/elixir
~/.nix-profile -> /nix/var/nix/profiles/per-user/***/profile
profile-56-link -> /nix/store/5yw8dnp9908ia6sdfvx01jzis4l2hni7-user-environment

Nix as a declarative configuration manager

{ config, pkgs, ... }:{
network.hostname = "nixos-test";
time.timeZone = "Europe/Paris";
# Omitting a lot of options, this is just a sample. environment.systemPackages = with pkgs; [
curl
git
gnupg
htop
...
];
}
# nixos-rebuild switch

Nix as a reproducible environment builder

{ pkgs ? import <nixpkgs> {} }:with pkgs;mkShell {
buildInputs = [ ocaml git ];
}

Using Nix

$ curl https://nixos.org/nix/install | sh
  • the single-user installation, where /nix/store is owned by the user installing Nix. This is the simplest way to install Nix if you are the only user on your machine and don’t want to use Nix extensively outside of setting your development environment. It is also the easiest to uninstall as you just have to delete the /nix directory;
  • the multi-user installation, where /nix/store is owned by root and a nixbld group has write access to it. All Nix operations are then performed by nix-daemon. Different users can use Nix simultaneously and you gain a system environment like on NixOS. In the multi-user installation, builds are performed by special builder users in complete isolation. It is a little bit more complex to manage, but this is the recommended way to install Nix if you plan to use it a lot, even if you are the only user on your machine. It is supported on macOS and all Linux running systemd with SELinux disabled.
$ sh <(curl https://nixos.org/nix/install) --no-daemon
$ sh <(curl https://nixos.org/nix/install) --daemon
# This defines a function taking `pkgs` as parameter, and uses
# `nixpkgs` by default if no argument is passed to it.
{ pkgs ? import <nixpkgs> {} }:
# This avoids typing `pkgs.` before each package name.
with pkgs;
# Defines a shell.
mkShell {
# Sets the build inputs, i.e. what will be available in our
# local environment.
buildInputs = [ elixir git ];
}
$ nix-shell
$ nix-shell --pure
$ nix-env -i <package>
$ nix-env -i nox
$ nox gcc
1 avr-gcc-8.2.0 (nixpkgs.avrgcc)
GNU Compiler Collection, version 8.2.0 for AVR microcontrollers
2 gcc-wrapper-7.3.0 (nixpkgs.gcc)
GNU Compiler Collection, version 7.3.0 (wrapper script)
3 gcc-arm-embedded-6-2017-q2-update (nixpkgs.gcc-arm-embedded)
Pre-built GNU toolchain from ARM Cortex-M & Cortex-R processors (Cortex-M0/M0+/M3/M4/M7, Cortex-R4/R5/R7/R8)
4 gcc-7.3.0 (nixpkgs.gcc-unwrapped)
GNU Compiler Collection, version 7.3.0
5 gcc-wrapper-4.8.5 (nixpkgs.gcc48)
GNU Compiler Collection, version 4.8.5 (wrapper script)
6 gcc-wrapper-5.5.0 (nixpkgs.gcc5)
GNU Compiler Collection, version 5.5.0 (wrapper script)
7 gcc-wrapper-6.4.0 (nixpkgs.gcc6)
GNU Compiler Collection, version 6.4.0 (wrapper script)
8 stdenv-darwin (nixpkgs.gcc7Stdenv)
The default build environment for Unix packages in Nixpkgs
9 gcc-wrapper-8.2.0 (nixpkgs.gcc8)
GNU Compiler Collection, version 8.2.0 (wrapper script)
...
Packages to install:

Keeping nixpkgs up to date

$ nix-channel --update
let
pkgs = import (fetchTarball {
url = https://github.com/NixOS/nixpkgs/archive/<rev>.tar.gz;
}) {};
# I’m also showing here you can define any variable.
elixir = pkgs.beam.packages.erlangR20.elixir_1_6;
in
with pkgs;mkShell {
# `elixir` is not the latest Elixir anymore, but the latest
# version of the 1.6 branch built on the latest Erlang 20 at the
# time when `nixpkgs` was at <commit-hash>, as stated above.
# Same for the version of Git.
buildInputs = [ elixir git ];
}

Collecting garbage

$ nix-collect-garbage
$ nix-collect-garbage -d
$ nix-collect-garbage --delete-older-than 30d
$ nix-store --optimise -v
$ mkdir -p ~/.config/nix
$ echo "auto-optimise-store = true" >> ~/.config/nix/nix.conf

direnv

$ nix-env -i direnv
eval "$(direnv hook <shell>)"
$ direnv edit .
use nix
$ direnv allow
  • the environment can take some time to build each time you run a shell in a project directory;
  • the environment is not (yet) persistent: garbage collection would delete it from the Nix store.
keep-outputs = true
keep-derivations = true
$ nix-env -i coreutils

A few words about comfort

$ git status
$ git checkout master
$ nix-shell --pure
$ gcm
$ nisp

Conclusion

--

--

--

Software Engineer and Film Photographer, passionate about many subjects including Embedded Systems, IT Security, Programming Languages, Music and more.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

C Programming- Algorithm and Flowchart

AWS Client VPN — Setup, Cost and OKTA SSO Integration

Why are most companies using cloud computing? ☁️

Reactive Programming in Java: How, Why, and Is It Worth Doing? Part II. Reactive approach

The most effective method to prevail in AWS Certified Solutions Architect — Associate exam

How to build Python Dashboards using Dash?

Cloud FinOps — Part 3: Cloud Cost Report

AWS EKS — working with AWS EKS from Mirantis Lens IDE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jean-Philippe Cugnet

Jean-Philippe Cugnet

Software Engineer and Film Photographer, passionate about many subjects including Embedded Systems, IT Security, Programming Languages, Music and more.

More from Medium

Gitty Workflow

How To Containerize And Scale Your Application For Unexpected Traffic Or Model Training

How to deploy a Camunda Spring Boot application to an external application server

Something you should know — Web Framework