public:: true

  • Install

  • How nix work

    • Nix Store everything in the nix store usually at /nix/store

    • Nix profiles make the package visible to the user, a profile describes an environment normally don with nix-env

      nix-env -i package_name.  ## install a package in the default profile
      nix-env --query "*"       ## query packages in the current profile
      nix-env -e package_name.  ## remove a package in the current profile
      nix-env --rollback.       ## rollback to a previous generation
    • Channels: is a repository with packages for nix

      nix-channel --list
      nix-channel --add https://nixos.org/channels/nixpkgs-unstable unstable
      nix-channel --update
       
      nix-env -iA unstable.simgrid  ## Install simgrid from the unstable
    • packages

      nix-env upgrade simgrid
      nix search nixpkgs simgrid
    • Shells: makes visible packages of an specific profile to a new shell (like virtualenv)

      nix-shell
      
    • echo ‘experimental-features = nix-command flakes’ >> /etc/nix/nix.conf

  • Nix as build tool

    • Nix can be used as a tool to build software in a consistent manner

      {
      pkgs ? import (fetchTarball {
        url = "https://github.com/NixOS/nixpkgs/archive/4fe8d07066f6ea82cda2b0c9ae7aee59b2d241b3.tar.gz";
        sha256 = "sha256:06jzngg5jm1f81sc4xfskvvgjy5bblz51xpl788mnps1wrkykfhp";
      }) {}
      }:
      pkgs.stdenv.mkDerivation rec {
      pname = "chord";
      version = "0.1.0";
      
      src = pkgs.fetchgit {
        url = "https://gitlab.inria.fr/nix-tutorial/chord-tuto-nix-2022";
        rev = "069d2a5bfa4c4024063c25551d5201aeaf921cb3";
        sha256 = "sha256-MlqJOoMSRuYeG+jl8DFgcNnpEyeRgDCK2JlN9pOqBWA=";
      };
      
      buildInputs = [
        pkgs.simgrid
        pkgs.boost
        pkgs.cmake
      ];
      
      configurePhase = ''
        cmake .
      '';
      
      buildPhase = ''
        make
      '';
      
      installPhase = ''
        mkdir -p $out/bin
        mv chord $out/bin
      '';
      }
      
      nix-build chord.nix
      
  • Build to create a docker image

    • {
      pkgs ? import (fetchTarball {
        url = "https://github.com/NixOS/nixpkgs/archive/4fe8d07066f6ea82cda2b0c9ae7aee59b2d241b3.tar.gz";
        sha256 = "sha256:06jzngg5jm1f81sc4xfskvvgjy5bblz51xpl788mnps1wrkykfhp";
      }) {}
      }:
      
      with pkgs;
      
      let
      packages = rec {
        chord-docker = dockerTools.buildImage {
          name = "chord-docker";
          tag = "tuto-nix";
          contents = [ chord ];
          config = {
            Cmd = [ "${chord}/bin/chord" ];
            WorkingDir = "/data";
            Volumes = { "/data" = { }; };
          };
        };
      };
      in
      packages
      
      nix-build default.nix -A chord-docker
      

      This will generate a result tarball thar we need to load into docker

      docker load < result
      
  • Example of how to use nix-shell

    collapsed:: true
    •  This shell defines a development environment for the Chord project.
      {
      pkgs ? import (fetchTarball {
        url = "https://github.com/NixOS/nixpkgs/archive/4fe8d07066f6ea82cda2b0c9ae7aee59b2d241b3.tar.gz";
        sha256 = "sha256:06jzngg5jm1f81sc4xfskvvgjy5bblz51xpl788mnps1wrkykfhp";
      }) {}
      }:
      pkgs.mkShell rec {
       buildInputs = with pkgs; [
        cmake
        boost
        simgrid
      
        # debugging tools
        gdb
        valgrind
       ];
      }
      
      nix-shell shell.nix
      
  • What is a Flake

    • flake is just lockfiles for your nix configuration
    • since nix uses channels, is not 100% reproducible… you change the channels and your packages may or may not work.
    • with flakes… it will generate a lock files to ensure specific packages were used
    • to autocomplete my nix-darwin configuration I use this .nixd.json
    • {
          "options": {
              "enable": true,
              "target": {
                  "args": [
                      "--expr",
                      "(import <darwin> { configuration = ./darwin-configuration.nix; }).options"
                  ],
                  "installable": ""
              }
          }
      }
      
  • What is home-manager

    • Home manager is a way to configure all user configuration
    • from which applications are visible to this user
    • to modify $HOME files
  • What is NixOS

    • an linux distribution that everything is configured with nix
    • modify the /etc/nixos/configuration.nix file
    • and execute nixos-rebuild switch
    • if for some reason, something fails, you can rollback to previous versions, and if the kernel crashes, the bootloader shows you all previous configurations to boot from
  • Nix LSP

    • I like to use nixd https://github.com/nix-community/nixd
    • you can install it with
      • nix-env -iA nixpkgs.nixd
        
    • To configure it for your use, need a .nixd.json file that tell how to import
  • Configuration syntax

    • { config, pkgs, ... }:
      
      {
        environment.shells = [ pkgs.zsh ];
        
        homebrew.
        # List packages installed in system profile. To search by name, run:
        # $ nix-env -qaP | grep wget
        environment.systemPackages =
          [ pkgs.neovim
          ];
      
        # Use a custom configuration.nix location.
        # $ darwin-rebuild switch -I darwin-config=$HOME/.config/nixpkgs/darwin/configuration.nix
        # environment.darwinConfig = "$HOME/.config/nixpkgs/darwin/configuration.nix";
      
        # Auto upgrade nix package and the daemon service.
        services.nix-daemon.enable = true;
        # nix.package = pkgs.nix;
      
        # Create /etc/zshrc that loads the nix-darwin environment.
        programs.zsh.enable = true;  # default shell on catalina
        # programs.fish.enable = true;
      
        # Used for backwards compatibility, please read the changelog before changing.
        # $ darwin-rebuild changelog
        system.stateVersion = 4;
      }
      
    • { config, pkgs, ... }: {} means!
      • the whole file is a function, with a single parameter
      • this parameter is a set that need to have config and pkgs keys
      • and the function returns a new set
    • with
      • environment.shells = [ pkgs.zsh ]; could be rewritten as environment.shells = with pkgs; [ zsh ];
    • inherith
    • import imports a module, could be a relative file, or a search in nix path file with < > angular brackets