1
1
mirror of https://github.com/nmattia/niv.git synced 2024-11-07 22:36:53 +03:00
Easy dependency management for Nix projects
Go to file
2021-01-05 11:51:20 +01:00
.github Don't push drv files to cachix 2020-09-07 16:13:30 +02:00
app Fight cabal 2019-06-12 16:26:13 +02:00
examples/cpp-libosmium Drop nixpkgs-channels in README, usage and examples 2020-07-13 21:59:11 +02:00
foo Fix foo to get back static executable 2020-02-25 17:46:35 +01:00
nix Turn NIV_OVERRIDE values into paths 2020-10-05 11:39:37 +02:00
script Add --rev and -r 2020-08-21 16:17:27 +02:00
site Add update script 2019-09-09 20:35:49 +02:00
src Add flag for --no-colors 2021-01-05 11:51:20 +01:00
tests Prepend environment variables with NIV_ 2020-09-17 13:29:09 +02:00
.gitignore Add some more things to gitignore 2019-06-15 16:06:19 +01:00
CHANGELOG.md Update CHANGELOG.md 2020-11-02 14:42:14 +01:00
default.nix Show version with --version 2020-09-07 16:14:10 +02:00
LICENSE Prepare for release 2019-06-13 11:30:13 +02:00
package.yaml Release 0.2.18 2020-09-17 13:32:07 +02:00
README.md Add flag for --no-colors 2021-01-05 11:51:20 +01:00
README.tpl.md Fix README* 2020-11-02 14:42:14 +01:00
shell.nix Replace snack with cabal 2019-04-07 12:29:24 +02:00

niv

GitHub Actions Netlify Status

Painless dependencies for Nix projects. Read more in the Getting started section below.

Install

niv is available in nixpkgs as niv:

$ nix-env -iA nixpkgs.niv

Alternatively, run the following command to install the development version:

$ nix-env -iA niv -f https://github.com/nmattia/niv/tarball/master \
    --substituters https://niv.cachix.org \
    --trusted-public-keys niv.cachix.org-1:X32PCg2e/zAm3/uD1ScqW2z/K0LtDyNV7RdaxIuLgQM=

Build

Inside the provided nix shell:

$ repl

Run the test suite with this command:

$ ./script/test

Usage

niv simplifies adding and updating dependencies in Nix projects. It uses a single file, nix/sources.json, where it stores the data necessary for fetching and updating the packages.

  • Add: inserts a package in nix/sources.json.
  • Update: updates one or all packages in nix/sources.json.
  • Drop: deletes a package from nix/sources.json.

niv has some utility functions:

  • Init: bootstraps a Nix project, in particular creates a nix/sources.json file containing niv and nixpkgs as well as a nix/sources.nix file that returns the sources as a Nix object.
  • Show: shows the packages' information.
  • Modify: modifies attributes without performing an update.

Configuration

The following environment variables are read by niv:

Name Note
GITHUB_TOKEN or NIV_GITHUB_TOKEN When set, the value is used to authenticate GitHub API requests.
GITHUB_HOST or NIV_GITHUB_HOST The GitHub host to use when fetching packages. Port may be appended here.
GITHUB_API_HOST or NIV_GITHUB_API_HOST The host used when performing GitHub API requests. Use GITHUB_API_PORT for specifying the port.
GITHUB_API_PORT or NIV_GITHUB_API_PORT The port used when performing GitHub API requests. Defaults to 443 for secure requests. Defaults to 80 for insecure requests. See also: GITHUB_INSECURE.
NIV_GITHUB_INSECURE When set to anything but the empty string, requests are performed over http instead of https.
NIV_GITHUB_PATH The base path used when performing GitHub API requests.

The next two sections cover common use cases and full command description.

Getting started

This section covers common use cases:

Bootstrapping a Nix project

Use the init command when starting a new Nix project or when porting an existing Nix project to niv:

$ niv init
...
$ tree
.
└── nix
    ├── sources.json
    └── sources.nix

1 directory, 2 files

The file nix/sources.json is the file used by niv to store versions and is initialized with niv and nixpkgs:

{
    "nixpkgs": {
        "url": "https://github.com/NixOS/nixpkgs/archive/109a28ab954a0ad129f7621d468f829981b8b96c.tar.gz",
        "owner": "NixOS",
        "branch": "nixos-19.09",
        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
        "repo": "nixpkgs",
        "sha256": "12wnxla7ld4cgpdndaipdh3j4zdalifk287ihxhnmrzrghjahs3q",
        "description": "Nix Packages collection",
        "rev": "109a28ab954a0ad129f7621d468f829981b8b96c"
    },
    "niv": {
        "homepage": "https://github.com/nmattia/niv",
        "url": "https://github.com/nmattia/niv/archive/72e77204544527279e8f1e2d982d29503482b8f4.tar.gz",
        "owner": "nmattia",
        "branch": "master",
        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
        "repo": "niv",
        "sha256": "1zjcyzxhq9iwxh58j5d7sx1vz5s3r1f6gpmnfgj2a3rxmclwvn3c",
        "description": "Easy dependency management for Nix projects",
        "rev": "72e77204544527279e8f1e2d982d29503482b8f4"
    }
}

To use those dependencies import the file nix/sources.nix, e.g.:

{ sources ? import ./sources.nix }:     # import the sources
with
  { overlay = _: pkgs:
      { niv = import sources.niv {};    # use the sources :)
      };
  };
import sources.nixpkgs                  # and use them again!
  { overlays = [ overlay ] ; config = {}; }

Tracking a nixpkgs branch

The init command sets the nix/sources.json file to track the latest commit present on nixpkgs 19.09 when the command was run. Run the following command to update it:

$ niv update nixpkgs

To change the branch being tracked run this command:

$ niv update nixpkgs -b nixos-19.09     # equivalent to --branch nixos-19.09

Importing packages from GitHub

The add command will infer information about the package being added, when possible. This works very well for GitHub repositories. Run this command to add jq to your project:

$ niv add stedolan/jq

The following data was added in nix/sources.json for jq:

{
  "homepage": "http://stedolan.github.io/jq/",
  "url": "https://github.com/stedolan/jq/archive/9fa2e51099c55af56e3e541dc4b399f11de74abe.tar.gz",
  "owner": "stedolan",
  "branch": "master",
  "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
  "repo": "jq",
  "sha256": "0819rvk8057qgcqvgn7fpldvly2pfdw9fxcjrlqa8gr59p8a1cic",
  "description": "Command-line JSON processor",
  "rev": "9fa2e51099c55af56e3e541dc4b399f11de74abe"
}

Using custom URLs

It is possible to use niv to fetch packages from custom URLs. Run this command to add the Haskell compiler GHC to your nix/sources.json:

$ niv add ghc   \
    -v 8.4.3    \
    -t 'https://downloads.haskell.org/~ghc/<version>/ghc-<version>-i386-deb8-linux.tar.xz'

The option -v sets the "version" attribute to 8.4.3. The option -t sets a template that can be reused by niv when fetching a new URL (see the documentation for add and update).

The type of the dependency is guessed from the provided URL template, if -T is not specified.

For updating the version of GHC used run this command:

$ niv update ghc -v 8.6.2

Commands

niv - dependency manager for Nix projects

version: 0.2.18

Usage: niv [-s|--sources-file FILE] [--no-colors] COMMAND

Available options:
  -s,--sources-file FILE   Use FILE instead of nix/sources.json
  --no-colors              Don't use colors in output
  -h,--help                Show this help text
  --version                Print version

Available commands:
  init                     Initialize a Nix project. Existing files won't be
                           modified.
  add                      Add a GitHub dependency
  show                     
  update                   Update dependencies
  modify                   Modify dependency attributes without performing an
                           update
  drop                     Drop dependency

Add

Examples:

  niv add stedolan/jq
  niv add NixOS/nixpkgs -n nixpkgs -b nixpkgs-unstable
  niv add my-package -v alpha-0.1 -t http://example.com/archive/<version>.zip

Usage: niv add PACKAGE [-n|--name NAME] ([-a|--attribute KEY=VAL] |
               [-s|--string-attribute KEY=VAL] | [-b|--branch BRANCH] |
               [-o|--owner OWNER] | [-r|--rev REV] | [-v|--version VERSION] |
               [-t|--template URL] | [-T|--type TYPE])
  Add a GitHub dependency

Available options:
  -n,--name NAME           Set the package name to <NAME>
  -a,--attribute KEY=VAL   Set the package spec attribute <KEY> to <VAL>, where
                           <VAL> may be JSON.
  -s,--string-attribute KEY=VAL
                           Set the package spec attribute <KEY> to <VAL>.
  -b,--branch BRANCH       Equivalent to --attribute branch=<BRANCH>
  -o,--owner OWNER         Equivalent to --attribute owner=<OWNER>
  -r,--rev REV             Equivalent to --attribute rev=<REV>
  -v,--version VERSION     Equivalent to --attribute version=<VERSION>
  -t,--template URL        Used during 'update' when building URL. Occurrences
                           of <foo> are replaced with attribute 'foo'.
  -T,--type TYPE           The type of the URL target. The value can be either
                           'file' or 'tarball'. If not set, the value is
                           inferred from the suffix of the URL.
  -h,--help                Show this help text

Experimental commands:
  git                      Add a git dependency. Experimental.
  github                   Add a GitHub dependency
  local                    Add a local dependency. Experimental.

Update

Examples:
  
  niv update                     # update all packages
  niv update nixpkgs             # update nixpkgs
  niv update my-package -v beta-0.2 # update my-package to version "beta-0.2"

Usage: niv update [PACKAGE] ([-a|--attribute KEY=VAL] |
                  [-s|--string-attribute KEY=VAL] | [-b|--branch BRANCH] |
                  [-o|--owner OWNER] | [-r|--rev REV] | [-v|--version VERSION] |
                  [-t|--template URL] | [-T|--type TYPE])
  Update dependencies

Available options:
  -a,--attribute KEY=VAL   Set the package spec attribute <KEY> to <VAL>, where
                           <VAL> may be JSON.
  -s,--string-attribute KEY=VAL
                           Set the package spec attribute <KEY> to <VAL>.
  -b,--branch BRANCH       Equivalent to --attribute branch=<BRANCH>
  -o,--owner OWNER         Equivalent to --attribute owner=<OWNER>
  -r,--rev REV             Equivalent to --attribute rev=<REV>
  -v,--version VERSION     Equivalent to --attribute version=<VERSION>
  -t,--template URL        Used during 'update' when building URL. Occurrences
                           of <foo> are replaced with attribute 'foo'.
  -T,--type TYPE           The type of the URL target. The value can be either
                           'file' or 'tarball'. If not set, the value is
                           inferred from the suffix of the URL.
  -h,--help                Show this help text

Modify

Examples:

  niv modify nixpkgs -v beta-0.2
  niv modify nixpkgs -a branch=nixpkgs-unstable

Usage: niv modify PACKAGE [-n|--name NAME] ([-a|--attribute KEY=VAL] |
                  [-s|--string-attribute KEY=VAL] | [-b|--branch BRANCH] |
                  [-o|--owner OWNER] | [-r|--rev REV] | [-v|--version VERSION] |
                  [-t|--template URL] | [-T|--type TYPE])
  Modify dependency attributes without performing an update

Available options:
  -n,--name NAME           Set the package name to <NAME>
  -a,--attribute KEY=VAL   Set the package spec attribute <KEY> to <VAL>, where
                           <VAL> may be JSON.
  -s,--string-attribute KEY=VAL
                           Set the package spec attribute <KEY> to <VAL>.
  -b,--branch BRANCH       Equivalent to --attribute branch=<BRANCH>
  -o,--owner OWNER         Equivalent to --attribute owner=<OWNER>
  -r,--rev REV             Equivalent to --attribute rev=<REV>
  -v,--version VERSION     Equivalent to --attribute version=<VERSION>
  -t,--template URL        Used during 'update' when building URL. Occurrences
                           of <foo> are replaced with attribute 'foo'.
  -T,--type TYPE           The type of the URL target. The value can be either
                           'file' or 'tarball'. If not set, the value is
                           inferred from the suffix of the URL.
  -h,--help                Show this help text

Drop

Examples:

  niv drop jq
  niv drop my-package version

Usage: niv drop PACKAGE [ATTRIBUTE]
  Drop dependency

Available options:
  -h,--help                Show this help text

Init

Usage: niv init ([--no-nixpkgs] | [-b|--nixpkgs-branch ARG]
                [--nixpkgs OWNER/REPO])
  Initialize a Nix project. Existing files won't be modified.

Available options:
  --no-nixpkgs             Don't add a nixpkgs entry to sources.json.
  -b,--nixpkgs-branch ARG  The nixpkgs branch to use. (default: "release-20.03")
  --nixpkgs OWNER/REPO     Use a custom nixpkgs repository from
                           GitHub. (default: NixOS/nixpkgs)
  -h,--help                Show this help text

show

Usage: niv show [PACKAGE]

Available options:
  -h,--help                Show this help text

Frequently Asked Questions

Can I use private GitHub repositories?

Yes. There are two ways:

1. Use the git protocol

When using the git protocol, your public SSH keypair is used to authenticate you:

$ niv add git git@github.com:my_user/my_private_repo
2. Use the netrc file

in order to niv add a private github repo you'll need to:

  1. create a .netrc file with the following content
machine github.com
  login YOUR_GITHUB_USER_NAME
  password YOUR_GITHUB_TOKEN
  1. add the path to the above file to /etc/nix/nix.conf:
netrc-file = /PATH/TO/.netrc
  1. set GITHUB_TOKEN env var when calling niv add
GITHUB_TOKEN=$YOUR_GITHUB_TOKEN niv add ...

How do I import a subpath of a source?

In order to use the directory dir of a my-package, use the following pattern:

let
  sources = import ./nix/sources.nix;
in sources.my-package + "/dir"

in this example, sources.my-package becomes my-package's root directory, and + "/dir" appends the subdirectory.

How do I import NixOS modules?

After the package containing the modules has been niv added, importing the modules is straightforward:

let
  sources = import ./nix/sources.nix;
in {
  imports = [ (sources.package + "/path/to/module") ];
}

Can I use local packages?

If you need to use a local path as a source -- especially convenient when modifying dependencies -- niv allows you to override the sources.json via environment variables. To override a source foo with a local path ./bar/baz, set the environment variable NIV_OVERRIDE_foo to ./bar/baz.

Generally, if the environment variable NIV_OVERRIDE_<name> is set and you have a source named <name> then niv will use the value of NIV_OVERRIDE_<name> as the outPath of that source. All non-alphanumeric characters in the source name are escaped to the character _; i.e. to override the package my package-foo you need to set the environment variable NIV_OVERRIDE_my_package_foo.