#packages #nixos #install #tool #nix #configuration #parse

bin+lib npkg

Some functions to manage and install packages on NixOS

3 releases

0.1.2 Jun 25, 2022
0.1.1 May 20, 2022
0.1.0 May 12, 2022

#25 in #nixos

MIT license

53KB
1.5K SLoC

Npkg

Built with Nix License: MIT

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 and edit 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!

NixOS Installation

git clone https://github.com/vlinkz/npkg
nix-env -f npkg -i npkg

Then modify ~/npkg/config.json to match your configuration.

Usage with Nix Flakes

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

Declarative System Installation

Head of configuration.nix

{ config, pkgs, lib, ... }:

let
  npkg = (import (pkgs.fetchFromGitHub {
    owner = "vlinkz";
    repo = "npkg";
    rev = "0.1.2";
    sha256 = "0000000000000000000000000000000000000000000000000000";
  })).default;
in 
{
  imports =
# rest of configuration

Packages:

environment.systemPackages =
with pkgs; [
  npkg
  # rest of your packages
];

Arguments

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 '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 'configuration.nix'
    -u, --update             Update packages
    -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.

  • 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.

  • 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.

Updating packages

To update all packages:

npkg -u

To specify only one type, 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) (nix env)
  A program that produces a familiar, friendly greeting

This means that package hello version 2.12 is currently installed with nix-env.

Configuration

A configuration file is stored in ~/.config/npkg/config.json, by default, it contains:

{
  "systemconfig": "/etc/nixos/configuration.nix",
  "homeconfig": "/home/$HOME/.config/nixpkgs/home.nix",
  "flake": null
}

These values can be edited to point to other locations. This is useful in nix flake based systems or any system where config files are not in expected locations.

But why?

I wanted to code something as a proof of concept for using nix-editor as a backed for other tools. But so far I've been using it almost daily!

Future plans

  • 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

Dependencies

~19–27MB
~595K SLoC