Npkg: All in one NixOS package management

So these past couple of days, in the free time that I had, I decided to test out some of the use cases of a NixOS configuration editor I wrote nix-editor. So I started writing out a basic package management utility and before I knew it I had spent all day working on it. I think for the short timeframe I had, it turned out pretty well! So presenting :drum: :drum: Npkg! It’s still very much WIP (probably won’t work on some of your wacky configs), but my main goal is to use this as a starting point for working on more utilities (including some guis I’m working on) that make using NixOS easier for new users. Below is just the readme from the repo. Hope you try it out!

Npkg

Npkg is a small tool that allows you to configure all your NixOS packages in one place.

Npkg is written with rust and uses nix-editor and rnix-parser to parse configuration files.

WARNING

This tool is still new/experimental, and being that it directly modifies critical files, such as /etc/nixos/configuration.nix, make sure you have backups in case it messes it up or deletes such files. I’ve already sacrificed some of my files to the void, don’t let that happen to you!

Usage with Nix Flakes

nix run github:vlinkz/npkg -- --help
npkg 0.0.3

USAGE:
    npkg [OPTIONS] [PACKAGES]...

ARGS:
    <PACKAGES>...    Packages

OPTIONS:
    -d, --dry-run            Do not build any packages, only edit configuration file
    -E, --env                Use nix environment 'nix-env'
    -h, --help               Print help information
    -H, --home               Use home-manager '~/.config/nixpkgs/home.nix'
    -i, --install            Install a package
    -l, --list               List installed packages
    -o, --output <OUTPUT>    Output modified configuration file to a specified location
    -r, --remove             Remove a package
    -s, --search             Search for a package
    -S, --system             Use system '/etc/nixos/configuration.nix'
    -V, --version            Print version information

Use cases

Installing packages

To install a package, you can run:

npkg -i <PACKAGE>

By default, this will use nix-env and install the package in you current environment. You can choose to use a specific available installer by using the -S, -H, or -E flags.

  • sudo npkg -iS hello
    

    will install the hello package as a system package by modifying your /etc/nixos/configuration.nix file and then calling nixos-rebuild switch.

  • npkg -iH hello
    

    will install the hello package using home-manager if it is installed. It will modify ~/.config/nixpkgs/home.nix and then call home-manager switch.

  • sudo npkg -iE hello
    

    will install the hello package to the current nix environment by calling nix-env -iA nixos.hello.

Removing packages

Very similar to installing packages:

npkg -r <PACKAGE>

The same -S, -H, and -E flags apply.

List installed packages

npkg -l

This will list all packages installed in /etc/nixos/configuration.nix, ~/.config/nixpkgs/home.nix, and with nix-env.

You can specify only one of these by using the -S, -H, and -E flags.

Search for a package

npkg -s <QUERY>

This will print a list of packages that match the query specified. For example:

$ npkg -s hello greeting

* hello (2.12)
  A program that produces a familiar, friendly greeting

But why?

I wanted to code something as a proof of concept for using nix-editor as a backed for other tools. Plus I had nothing better to do on a Saturday. Although given my limited time using this, I think I’ll stick to it for a while.

Future plans

  • Add update command: npkg -u

    • Maybe update channels as well? :eyes:
  • Check for installed packages in other locations.

    For example, if installing hello with home-manager, and it’s already installed with nix-env, give an option to switch it over.

  • When removing packages, automatically detect where installed instead of defaulting to nix-env

  • Whatever else pops into my head

4 Likes

I currently don’t use NixOS but will install it on my new laptop which should arrive in e few weeks. And I will DEFINITIVELY use this tool!! I used NixOS for over a year and always struggled with keeeping my env clean. I had way too much stuff in nix-env which should’ve been in my configuration.nix. And I was always too lazy trying to have an overview/fix it. That will change with this tool. :relaxed:

Thanks a lot for making this and PLEASE keep working on it!
I feel like this could become really beloved if it reaches a stable state! :slight_smile:

2 Likes