INTRODUCTION TO NIX DEVELOP

KunkkaFollow
Last update: 2025-11-12,
8 mins to read

Nix is a tool that takes a unique approach to package management and system configuration.

  • Nix has 3 main concepts:
  1. Reporducible
  2. Declarative
  3. Reliable

Nix develop

What is the nix develop

  • Starts a new bash shell with a defined set of deelopment dependencies creating a reproducible and isolated environment for building and testing a package.
  • It sets up environment variables and functions, allowing devs to run build commands and interact with their project's tools without affecting their system's main configuration.

What nix develop does

  • Creates an isolated development environment: it uses the devShell from a project's flake.nix file to set up a shell where only the specified dependencies are available. This prevents dependency conflicts with other projects or your system's global packages
  • Sets up necessary environment variables, its configures environment variables like PKG_CONFIG_PATH that are needed by tools to find their dependencies with in the isolated environment.
  • Provides a reproducible setup: because the environment is declared in a Nix flake file, it guarantees that any dev can spin up the exact same development environment with the same dependencies.
  • Enables interactive development: You can use the shell to run build commands --build, --check, ... in a controlled environment.
  • Runs commands in isolation from builds: by default, the dependencies available in nix develop are not available during a nix build or nix run, ensuring builds are performed with complete isolation from the development shell's dependencies.

Nix pkgs

You can search available packages by command:

nix search nixpkgs <pkg_name>

or easier, by this link:

Link

Nix flake

  • A file (flake.nix) to declare inputs and outputs, along with flake.lock to pin exact versions of dependencies for reproducible builds.
    • inputs: describe the other flakes that you would like to use; things like nixpkgs or home-manager. We need to give it the url where the code for that other flake is, and usually we use github.
    • outputs: is a function, which is where we can start getting into the nix language. Nix will go and fetch all the inputs, load up their flake.nix files, and it will call the outputs function with all their outputs as arguments. The outputs are just whatever its outputs function returns. Finally, nix records exactly which revision was fetched from Git in flake.lock so the versions of all inputs are pinned to the same thing until we manually update the lock file.
  • You can init flake by this command
nix flake init

Nix develop's advantages and disadvantages

Advantages

  • Reproducible: - We have lock file that helps to track the entire dependency chain, all the way down to libc. This ensures that running nix develop twice or on other machines procedues the exact same env deterministically. This is almost impossible with Dockerfile, due to mutable base images and apt-get update commands.
  • Light and Low overhead: Nix envs run natively on the host system, avoid the overhead of running a full VM. -> we have better env's performance and better resource efficiency.
  • Seamless host integration: We can easy to access to host level programs and configurations, while some tools like dev container always require complex volume mounts or workarounds for tight host integration.
  • Composable environment: share dependencies bw projects, computers are built only once and cached, saving time and diskspace.

Disadvantages

  • Learning curve
    • I am using nix darwin in an apple pc, using nixos in 2 machines, also using nixos in 2 VM, but I still don't understand so many things about nix/nixos
  • Diskspace usage
    • Nix/ nixos generate immutable data, so it will cost diskspace. Of course, you can claim your spaces with nix garbage collector but still.
  • CPU architectures, packages available
    • Some packages are not available in some specific CPU architectures, nix team needs to maintain a huge number of packages so your needed packages (if it's latest version) might not be able to use in nix

Appendix

  • flake.nix example:
{
  description = "nix development environment for 1st sample";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  };

  outputs =
    { nixpkgs, ... }:
    let
      systems = [
        "x86_64-linux"
        "aarch64-linux"
        "x86_64-darwin"
        "aarch64-darwin"
      ];
      forAllSystems = nixpkgs.lib.genAttrs systems;
    in
    {
      devShells = forAllSystems (
        system:
        let
          pkgs = import nixpkgs { inherit system; };
        in
        {
          default = pkgs.mkShell {

            packages = with pkgs; [
              jdk8
              nodejs_22
              gradle
              fastfetch
            ];
            shellHook = ''
              echo "Hello world"
              echo "Environment ready for ${system}"
              sh ./embedded.sh
            '';
          };
        }
      );
    };
}

Commands:

  • build env
nix develop
  • claim space
nix-collect-garbage -d

▶  Find out more: