aditya dot in

Beginner Nix

I’ve been using nixos for the past 6 months now and had a few small notes to share.

  1. Nix is:

    1. An Operating System: NixOS

    2. A language: A purely functional language used to write package derivations

      # Example Derivation
      pkgs.stdenv.mkDerivation {
      
        # name of our derivation
        name = "basic-derivation";
      
        # sources that will be used for our derivation.
        src = ./src;
      
        # see https://nixos.org/nixpkgs/manual/#ssec-install-phase
        # $src is defined as the location of our `src` attribute above
        installPhase = ''
          # $out is an automatically generated filepath by nix,
          # but it's up to you to make it what you need. We'll create a directory at
          # that filepath, then copy our sources into it.
          mkdir $out
          cp -rv $src/* $out
        '';
      }
    3. A package manager: nixpkgs

  2. The ideas of Nix are described in Dolstra’s PhD Thesis

  3. But in simpler terms I get:

    1. A stable package manager

    2. A stateful way to share configs and packages. Which also means portable

    3. This stateful config also means I don’t have to deal with ugly UI’s. I can see the package reference and define what and how I want things.

    4. Tools like nix-flakes and nix shells which are great to setup developer environments. If I wanted vi, clang and lapack for just a while I will: nix-shell -p vi clang lapack and eventually these packages will get GC’ed.

  4. Nix is HARD. It has:

    1. A learning curve

    2. Bad documentation/tutorials and error messages

    3. Convoluted toolchain

    4. Requires more space due to its design.

  5. Nix is easy. Despite the above criticisms, "iT WoRks For meee…​". No really, I’ve distrohopped, and although my personality is that of a Debian user(I think). I most of the times don’t need bleeding edge, give me stability and minimal breakage, if need be I will build from source and take that headache. I didn’t like how my debian worked nor how my fedora worked. I could have made it work if I tried but I didn’t. Then I found nix

    1. You don’t need to go all in and use nixos, you can install nix on your ubuntu/fedora/,.etc

    2. Once installed consider using Home Manager. The README has a section titled Words of Warning which you should read. Adding a package is as simple as

    3. Nixpkgs is developed on GitHub. It’s been praised to be as bleeding edge as arch but without the breakage. This matches my experience except I haven’t used arch. It does feel a little unsafe from the security perspective and we may see state and non-state actors attack package managers similar to XZ Utils backdoor but I’m not trying to scare you. This isn’t as bad as running and building random github repos from the web that some people do. Ultimately its a hard problem and my opinions aren’t of value at the moment.

Finally, If you are successful at using nix for a bit then it’s time that you learn about nix-flakes.