DEV Community

Abhishek Anand Amralkar
Abhishek Anand Amralkar

Posted on • Updated on

Manage Your Systems with Nix Package Manager

Nix Package Manager

We will walk through the Nix Package Manager and how we can build immutable systems. This is my first attempt to get Nix for all of my machines mostly Debian's(2 Laptops and 1 Desktop) and a MacBook.

The quickest way to install Nix is to open a terminal and run the following command (as a user other than root with sudo permission):

curl -L | sh

Add source $HOME/.nix-profile/etc/profile.d/ to your .bashrc or .zshrc.

echo 'source $HOME/.nix-profile/etc/profile.d/' >> ~/.bashrc

The Basics

After installation we have some new commands available:

* nix-env used to install dependencies and alter your current profile
* nix-shell used to start shells based on nix expressions
* nix-store used to query or manipulate the nix store

Install a Package

nix-env -iA nixpkgs.clojure 

# Output
installing 'clojure-'
building '/nix/store/y6r745pjimp232bgknncx27a074i4j0z-user-environment.drv'...
created 44 symlinks in user environment

Query a Package

nix-env -qaP 'consul'

# Output
nixpkgs.consul                      consul-1.7.2
nixpkgs.consul-alerts               consul-alerts-0.6.0
nixpkgs.consul-template             consul-template-0.19.4
nixpkgs.prometheus-consul-exporter  consul_exporter-0.3.0

Uninstall a Package

nix-env -e clojure

uninstalling 'clojure-'

Nix Shell

Nix-shell lets you open a shell in a new environment.

In Nix; an environment is a collection of derivations (aka functions) that are put into your PATH.

We can create an environment by creating a .nix file to define the environment. Create a file called consul.nix with below content


Nix is built around the concept of derivations. A derivation is simply defined as "a build action". It produces 1 (or maybe more) output paths in the nix store.

with import <nixpkgs> {};

  clj = pkgs.clojure;
stdenv.mkDerivation rec {
  name = "clojure-environment";

  buildInputs = [

  shellHook = ''
    echo "using clojure: ${}"

and finally login to your env shell

nix-shell packages/clojure.nix
using clojure: clojure-

To install packages from Non NixOS

{ pkgs ? import <nixpkgs> {} }:
with import <nixpkgs> {};

pkgs.stdenv.mkDerivation {
  name = "consul";

  src = pkgs.fetchurl {
    url = "";

    sha256 = "98df3e0a8ede84794fa4d20b1b6b5d52ad3b983dec916c4d612cecba7c48a421";
  phases = ["unpackPhase" "installPhase"];

  unpackPhase = ''


  installPhase = ''
    mkdir -p $out/bin
    ${pkgs.unzip}/bin/unzip $src -d $out/bin
    chmod +x $out/bin/consul


To run it

nix-env -f consul.nix -i

The stdenv

The standard build environment in the Nix Packages collection provides an environment for building Unix packages.

To build a package with the standard environment, you use the function stdenv.mkDerivation, instead of the primitive built-in function derivation, e.g.

with import <nixpkgs> {};

  py = pkgs.python;
stdenv.mkDerivation rec {
  name = "python-environment";

  buildInputs = [

  shellHook = ''
    echo "using python: ${}"

The standard environment provides the following packages:

    The GNU C Compiler, configured with C and C++ support.
    GNU coreutils (contains a few dozen standard Unix commands).
    GNU findutils (contains find).
    GNU diffutils (contains diff, cmp).
    GNU sed.
    GNU grep.
    GNU awk.
    GNU tar.
    gzip and bzip2.
    GNU Make. 
    The patch command.


    patch phase
    configure phase
    build phase
    check phase
    install phase
    buildup phase

Nix Expressions

Nix is not just a package manager. Nix is OS and a Language too.

The Nix expression language is a pure, lazy, functional language. Purity means that operations in the language don't have side-effects (for instance, there is no variable assignment). Laziness means that arguments to functions are evaluated only when they are needed. Functional means that functions are “normal” values that can be passed around and manipulated in interesting ways. The language is not a full-featured, general-purpose language. Its main job is to describe packages, compositions of packages, and the variability within packages.

To read more

In this blog, we just looked into just a small functionality of NIX. NIX is much more than that.

Happy Nixing!

Top comments (2)

ssimontis profile image
Scott Simontis

I don't think I understand any benefits here. I have to learn new tools versus using ones I am very familiar with, and I don't see any assistance provided when it comes to configuration file setup or producing repeatable builds/deployments.

mrturkmen profile image
Ahmet Turkmen

This blog post gives more valuable information how nix is providing more benefits.