Nix(OS) Cheatsheet

Updated Jun 15, 2025·Published Oct 3, 2024·1 minute read

    Contents

  1. Resources
  2. Using nix profile to install packages
  3. Using nix shell to create a temporary shell
  4. Development environments using nix develop
  5. Derivations with flakes
  6. NixOS using flakes

Resources #

Using nix profile to install packages #

nix profile install nixpkgs#<package name> # install a package
nix profile install nixpkgs/nixos-unstable#<package name> # install an unstable package
nix profile upgrade <package name> # upgrade a single package
nix profile upgrade --all # upgrade all installed packages
nix profile list # lists installed packages along with indicies
nix profile remove <package name> # remove a package

Using nix shell to create a temporary shell #

nix shell nixpkgs#<package name>
nix shell nixpkgs/nixos-unstable#<package name>

Development environments using nix develop #

A basic cross-platform development Flake, can look like:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };
  outputs =
    {
      nixpkgs,
      flake-utils,
      ...
    }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
      in
      {
        devShells.default = pkgs.mkShell {
          nativeBuildInputs = with pkgs; [
            # packages needed at build time
          ];
          buildInputs = with pkgs; [
            # libraries to link against
          ];
        };
      }
    );
}

Now you can enter the development environment by running:

nix develop                   # uses bash by default
nix develop --command "fish"  # use your favourite shell

Derivations with flakes #

nix build .#mypackage

If you push the repository to GitHub (or another Git host) you can run or install it on antoher machine:

nix profile install github:rijkvp/launchr
nix run github:rijkvp/launchr

Build a Rust crate using crane #

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    crane.url = "github:ipetkov/crane";
    flake-utils.url = "github:numtide/flake-utils";
  };
  outputs =
    {
      nixpkgs,
      crane,
      flake-utils,
      ...
    }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        craneLib = crane.mkLib pkgs;

        commonArgs = {
          src = craneLib.cleanCargoSource ./.;
          strictDeps = true;
        };

        crate = craneLib.buildPackage (
          commonArgs
          // {
            cargoArtifacts = craneLib.buildDepsOnly commonArgs;
          }
        );
      in
      {
        checks = {
          inherit crate;
        };
        packages.default = crate;
        apps.default = flake-utils.lib.mkApp {
          drv = crate;
        };
      }
    );
}

NixOS using flakes #

My favourite method of setting up a NixOS system is using flakes with Home Manager as a NixOS module. An example flake.nix could look like:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
    home-manager.url = "github:nix-community/home-manager";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";
    # ... other flake inputs
  };

  outputs = inputs@{ nixpkgs, home-manager, ... }: {
    nixosConfigurations = {
      myhost = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          ./configuration.nix
          home-manager.nixosModules.home-manager
          # ... other NixOS modules
          {
            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
            home-manager.users.myuser = ./home.nix;
            home-manager.backupFileExtension = "backup";
            home-manager.extraSpecialArgs = {
                # pass arguments to home.nix
            };
          }
        ];
      };
    };
  };
}

To switch to this configuration, use:

sudo nixos-rebuild switch --flake .#myhost

Assuming myhost is configured as an SSH host inside ~/.ssh/config, you can deploy a remote NixOS machine like:

sudo nixos-rebuild switch --flake .#myhost --target-host myhost --use-remote-sudo