Nix minimal setup - kdaisho/Blog GitHub Wiki

This is a quick guide for using Nix as a package manager.

Install

(new) deprecated -- I switched to use home-manager. No more shell.nix. Go to the link below.

See How to install Nix home manager



sh <(curl -L https://nixos.org/nix/install) --daemon

https://nixos.org/download/

Start nix

After successful installation, run nix.

Ubuntu

. /home/jaeger/.nix-profile/etc/profile.d/nix.sh

Kali

. /etc/profile.d/nix.sh

Install packages globally

nix-env -iA nixpkgs.packagename

List installed package

Ubuntu

When you run nix-env -q on Ubuntu, you'll see nix along with other installed packages (after installing nodejs and deno)

deno-2.1.9
nix-2.26.2
nodejs-22.13.1

Kali

But on Kali, you don't see nix in the list. This is actually normal behavior. It's because nix itself is not installed via nix-env. Instead, nix is installed system-wide through the installation script, typically in /nix/store with its binaries linked in system locations.

deno-2.1.9
nodejs-22.13.1

Verify your nix installation

nix --version

This will show you the version of nix that's currently installed on your system, even though it doesn't appear in thee nix-env -q output.

Here are some common commands to get you started:

  • Search for packages: nix search nixpkgs packagename
  • Install a package: nix-env -iA nixpkgs.packagename
  • List installed packages: nix-env -q
  • Remove a package: nix-env -e packagename
  • Update all packages: nix-env -u

(new) Undo global package installation, starting using shell.nix

I wanted to drop global level installation and have it user-level. It requires to have shell.nix file, but this has many benefits.

Uninstall global packages

nix-env -e deno

Define packages in shell.nix at user-root

Create shell.nix at (user) root, for example:

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = [
    pkgs.deno
    pkgs.nodejs_22
  ];
}

https://search.nixos.org/packages

Install packages

nix-shell

Why I did this? Because having a file (shell.nix) has benefits:

Keep Everything Organized in One File:

By using a shell.nix file, you can have a centralized place where you define all the packages you're using. This gives you a single view of all your dependencies (whether it's deno, nodejs, or other tools), which can be very handy for tracking what you've installed and ensuring everything is consistent.

Isolate Packages from the System:

If you're concerned about potential conflicts with system-wide packages (installed via apt), Nix lets you manage packages in an isolated environment. This way, you don't risk messing with the global system or interfering with other software on your machine.

Easily Switch Environments:

You can use the same shell.nix file across different systems or setups. If you need to recreate the same environment on another machine (or later down the line), you just copy over your shell.nix file, and it will bring the exact same versions of all the tools and libraries you've specified.

Reproducibility:

Even though you're using Nix for personal use, you still get the advantage of reproducibility. If you ever need to recreate your environment or share it with others (for example, another machine or project), your shell.nix file ensures that you can do that easily.

Miscellaneous

Want to know if you are in nix shell? You can dynamically modify the shell prompt.

Modify shell prompt

Shell.nix for Ubuntu

shell.nix

Shell.nix for Kali

shell.nix

⚠️ **GitHub.com Fallback** ⚠️