1
1
mirror of https://github.com/nmattia/niv.git synced 2024-09-16 01:47:08 +03:00
niv/README.md

545 lines
18 KiB
Markdown
Raw Normal View History

2019-01-17 23:59:01 +03:00
# niv
[![Test](https://github.com/nmattia/niv/actions/workflows/test.yml/badge.svg)](https://github.com/nmattia/niv/actions/workflows/test.yml)
2019-04-15 16:17:37 +03:00
[![Netlify Status](https://api.netlify.com/api/v1/badges/48532eaa-259f-4ca2-aadf-67f7c6b957fd/deploy-status)](https://niv.nmattia.com)
2019-02-01 00:05:45 +03:00
2019-06-15 17:44:20 +03:00
Painless dependencies for [Nix] projects. Read more in the [Getting started](#getting-started) section below.
2019-01-17 23:59:01 +03:00
2019-04-14 15:49:13 +03:00
<p align="center">
<img src="https://niv.nmattia.com/niv.svg">
</p>
* [Install](#install)
* [Build](#build)
* [Usage](#usage)
* [FAQ](#frequently-asked-questions)
2019-02-05 17:18:42 +03:00
## Install
`niv` is available in [`nixpkgs`](https://github.com/NixOS/nixpkgs) as `niv`:
``` bash
$ nix-env -iA nixpkgs.niv
```
Alternatively, run the following command to install the development version:
2019-02-05 17:18:42 +03:00
``` bash
2020-02-21 12:39:03 +03:00
$ 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=
2019-02-05 17:18:42 +03:00
```
## Build
2019-01-18 01:00:48 +03:00
Inside the provided nix shell:
``` bash
2019-04-05 12:43:57 +03:00
$ repl
2019-01-18 01:00:48 +03:00
```
Run the test suite with this command:
``` bash
$ ./script/test
```
2019-01-17 23:59:01 +03:00
## Usage
2019-02-08 21:29:48 +03:00
`niv` simplifies [adding](#add) and [updating](#update) 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](#add): inserts a package in `nix/sources.json`.
* [Update](#update): updates one or all packages in `nix/sources.json`.
* [Drop](#drop): deletes a package from `nix/sources.json`.
2020-01-20 18:55:25 +03:00
`niv` has some utility functions:
2019-02-08 21:29:48 +03:00
2019-04-15 00:50:42 +03:00
* [Init](#init): bootstraps a Nix project, in particular creates a
2019-02-08 21:29:48 +03:00
`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](#show): shows the packages' information.
2020-01-20 18:55:25 +03:00
* [Modify](#modify): modifies attributes _without_ performing an update.
2019-01-17 23:59:01 +03:00
2019-09-22 00:12:19 +03:00
### 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. |
2019-09-22 00:12:19 +03:00
The next two sections cover [common use cases](#getting-started) and [full command
description](#commands).
2019-06-15 17:44:20 +03:00
### Getting started
2019-02-11 22:33:10 +03:00
Nix is a very powerful tool for building code and setting up environments. `niv` complements it by making it easy to describe and update remote dependencies (URLs, GitHub repos, etc). It is a simple, practical alternative to [Nix flakes](https://nixos.wiki/wiki/Flakes).
2019-02-11 22:56:42 +03:00
This section covers common use cases:
2019-02-11 22:33:10 +03:00
* [Bootstrapping a Nix project](#bootstrapping-a-nix-project).
2019-02-11 23:23:39 +03:00
* [Tracking a different nixpkgs branch](#tracking-a-nixpkgs-branch).
2019-02-11 22:56:42 +03:00
* [Importing packages from GitHub](#importing-packages-from-github).
2019-02-11 22:33:10 +03:00
* [Fetching packages from custom URLs](#using-custom-urls).
#### Bootstrapping a Nix project
2019-06-15 17:44:20 +03:00
Use the `init` command when starting a new Nix project or when porting an
existing Nix project to niv:
2019-02-11 22:33:10 +03:00
``` shell
$ niv init
...
$ tree
.
2019-06-15 17:44:20 +03:00
└── nix
   ├── sources.json
   └── sources.nix
1 directory, 2 files
2019-02-11 22:33:10 +03:00
```
2019-02-11 22:56:42 +03:00
The file `nix/sources.json` is the file used by niv to store versions and is
initialized with nixpkgs:
2019-02-11 22:33:10 +03:00
``` json
{
"nixpkgs": {
"branch": "nixos-unstable",
"description": "Nix Packages collection",
"homepage": null,
2019-02-11 22:33:10 +03:00
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "6c43a3495a11e261e5f41e5d7eda2d71dae1b2fe",
"sha256": "16f329z831bq7l3wn1dfvbkh95l2gcggdwn6rk3cisdmv2aa3189",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/6c43a3495a11e261e5f41e5d7eda2d71dae1b2fe.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
2019-02-11 22:33:10 +03:00
}
}
```
To use this dependency, `import` the file `nix/sources.nix`, e.g.:
2019-02-11 22:33:10 +03:00
``` nix
2019-06-15 17:44:20 +03:00
{ sources ? import ./sources.nix }: # import the sources
import sources.nixpkgs # and use them again!
{ overlays = [] ; config = {}; }
2019-02-11 22:33:10 +03:00
```
For more information about importing sources to your nix files, check
the [frequently asked questions](#Frequently-asked-questions).
2019-02-11 22:33:10 +03:00
#### Tracking a nixpkgs branch
The `init` command sets the `nix/sources.json` to the content of the file
[data/nixpkgs.json](data/nixpkgs.json). Currently, you would be tracking the
`nixos-unstable` branch.
Run the following command to
update it to the last commit of the configured branch:
2019-02-11 22:33:10 +03:00
``` shell
$ niv update nixpkgs
```
To change the branch being tracked run this command:
``` shell
$ niv update nixpkgs -b master # equivalent to --branch master
2019-02-11 22:33:10 +03:00
```
#### 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:
``` shell
$ niv add stedolan/jq
```
The following data was added in `nix/sources.json` for `jq`:
``` json
{
"homepage": "http://stedolan.github.io/jq/",
2019-02-11 23:23:39 +03:00
"url": "https://github.com/stedolan/jq/archive/9fa2e51099c55af56e3e541dc4b399f11de74abe.tar.gz",
2019-02-11 22:33:10 +03:00
"owner": "stedolan",
"branch": "master",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
"repo": "jq",
2019-02-11 23:23:39 +03:00
"sha256": "0819rvk8057qgcqvgn7fpldvly2pfdw9fxcjrlqa8gr59p8a1cic",
2019-02-11 22:33:10 +03:00
"description": "Command-line JSON processor",
2019-02-11 23:23:39 +03:00
"rev": "9fa2e51099c55af56e3e541dc4b399f11de74abe"
2019-02-11 22:33:10 +03:00
}
```
#### 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`:
``` shell
$ 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](#add) and [update](#update)).
2020-11-02 15:45:59 +03:00
The type of the dependency is guessed from the provided URL template, if `-T`
is not specified.
2019-02-11 22:33:10 +03:00
2020-11-02 16:00:18 +03:00
For updating the version of GHC used run this command:
2019-02-11 22:33:10 +03:00
``` shell
$ niv update ghc -v 8.6.2
```
### Commands
2019-01-29 00:37:13 +03:00
```
2019-09-23 23:37:17 +03:00
niv - dependency manager for Nix projects
2023-03-12 14:44:53 +03:00
version: 0.2.22
2019-01-24 23:58:22 +03:00
2021-01-05 13:33:42 +03:00
Usage: niv [-s|--sources-file FILE] [--no-colors] COMMAND
2019-01-24 23:58:22 +03:00
2019-01-29 00:37:13 +03:00
Available options:
2019-12-09 20:34:50 +03:00
-s,--sources-file FILE Use FILE instead of nix/sources.json
2021-01-05 13:33:42 +03:00
--no-colors Don't use colors in output
2019-01-29 00:37:13 +03:00
-h,--help Show this help text
2020-09-07 17:01:29 +03:00
--version Print version
2019-01-24 23:58:22 +03:00
2019-01-29 00:37:13 +03:00
Available commands:
init Initialize a Nix project. Existing files won't be
modified.
2019-11-24 16:46:33 +03:00
add Add a GitHub dependency
2019-01-31 01:56:35 +03:00
show
2019-01-29 00:37:13 +03:00
update Update dependencies
2020-01-20 18:55:25 +03:00
modify Modify dependency attributes without performing an
update
2019-01-29 00:37:13 +03:00
drop Drop dependency
```
2019-01-17 23:59:01 +03:00
2019-02-11 22:33:10 +03:00
#### Add
2019-01-17 23:59:01 +03:00
```
2019-01-29 00:37:13 +03:00
Examples:
2019-01-17 23:59:01 +03:00
2019-01-29 00:37:13 +03:00
niv add stedolan/jq
2020-07-15 13:26:45 +03:00
niv add NixOS/nixpkgs -n nixpkgs -b nixpkgs-unstable
2019-01-29 00:37:13 +03:00
niv add my-package -v alpha-0.1 -t http://example.com/archive/<version>.zip
2019-01-17 23:59:01 +03:00
2021-05-11 15:15:20 +03:00
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)]
2019-11-24 16:46:33 +03:00
Add a GitHub dependency
2019-01-17 23:59:01 +03:00
2019-01-29 00:37:13 +03:00
Available options:
2019-01-31 22:44:10 +03:00
-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>.
2019-01-31 22:44:10 +03:00
-b,--branch BRANCH Equivalent to --attribute branch=<BRANCH>
-o,--owner OWNER Equivalent to --attribute owner=<OWNER>
-r,--rev REV Equivalent to --attribute rev=<REV>
2019-01-31 22:44:10 +03:00
-v,--version VERSION Equivalent to --attribute version=<VERSION>
2019-01-31 01:56:35 +03:00
-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.
2019-01-29 00:37:13 +03:00
-h,--help Show this help text
2019-01-31 01:56:35 +03:00
2019-11-24 13:57:05 +03:00
Experimental commands:
2019-11-24 16:46:33 +03:00
git Add a git dependency. Experimental.
github Add a GitHub dependency
2020-03-30 22:38:08 +03:00
local Add a local dependency. Experimental.
2019-01-17 23:59:01 +03:00
```
2019-02-11 22:33:10 +03:00
#### Update
2019-01-27 01:39:38 +03:00
2019-01-29 00:37:13 +03:00
```
Examples:
2019-11-10 18:03:33 +03:00
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"
2019-01-23 23:55:26 +03:00
2021-05-11 15:15:20 +03:00
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)]]
2019-01-29 00:37:13 +03:00
Update dependencies
2019-01-23 23:55:26 +03:00
2020-01-20 18:55:25 +03:00
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>
2020-01-20 18:55:25 +03:00
-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
2021-05-11 15:15:20 +03:00
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)]
2020-01-20 18:55:25 +03:00
Modify dependency attributes without performing an update
2019-01-29 00:37:13 +03:00
Available options:
2020-01-31 22:08:52 +03:00
-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>.
2019-01-31 22:44:10 +03:00
-b,--branch BRANCH Equivalent to --attribute branch=<BRANCH>
-o,--owner OWNER Equivalent to --attribute owner=<OWNER>
-r,--rev REV Equivalent to --attribute rev=<REV>
2019-01-31 22:44:10 +03:00
-v,--version VERSION Equivalent to --attribute version=<VERSION>
2019-01-31 01:56:35 +03:00
-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.
2019-01-29 00:37:13 +03:00
-h,--help Show this help text
```
2019-01-23 23:55:26 +03:00
2019-02-11 22:33:10 +03:00
#### Drop
2019-01-27 01:39:38 +03:00
2019-01-29 00:37:13 +03:00
```
Examples:
2019-01-27 01:39:38 +03:00
2019-01-29 00:37:13 +03:00
niv drop jq
2019-02-08 21:07:05 +03:00
niv drop my-package version
2019-01-27 01:39:38 +03:00
2019-02-08 21:07:05 +03:00
Usage: niv drop PACKAGE [ATTRIBUTE]
2019-01-29 00:37:13 +03:00
Drop dependency
2019-01-23 23:55:26 +03:00
2019-01-29 00:37:13 +03:00
Available options:
-h,--help Show this help text
```
2019-01-17 23:59:01 +03:00
2019-02-11 22:33:10 +03:00
#### Init
2019-02-08 21:29:48 +03:00
```
Usage: niv init [--fast | --latest | --nixpkgs OWNER/REPO
(-b|--nixpkgs-branch ARG) |
--no-nixpkgs]
2019-02-08 21:29:48 +03:00
Initialize a Nix project. Existing files won't be modified.
Available options:
--fast Use the latest nixpkgs cached at
'https://github.com/nmattia/niv/blob/master/data/nixpkgs.json'.
This is the default.
--latest Pull the latest unstable nixpkgs from NixOS/nixpkgs.
2021-05-11 15:15:20 +03:00
--nixpkgs OWNER/REPO Use a custom nixpkgs repository from GitHub.
-b,--nixpkgs-branch ARG The nixpkgs branch when using --nixpkgs ....
--no-nixpkgs Don't add a nixpkgs entry to sources.json.
2019-02-08 21:29:48 +03:00
-h,--help Show this help text
```
2019-02-11 22:33:10 +03:00
#### show
2019-02-08 21:29:48 +03:00
```
2019-07-01 18:29:37 +03:00
Usage: niv show [PACKAGE]
2019-02-08 21:29:48 +03:00
Available options:
-h,--help Show this help text
```
2019-01-18 00:01:09 +03:00
[Nix]: https://nixos.org/nix/
2019-02-11 22:33:10 +03:00
[jq]: https://stedolan.github.io/jq/
[GHC]: https://www.haskell.org/ghc/
## Frequently Asked Questions
2020-08-13 16:35:18 +03:00
* [Can I use private GitHub repositories?](#can-i-use-private-github-repositories)
* [How do I import and use the content of a source?](#how-do-i-import-and-use-the-content-of-a-source)
2020-08-13 16:35:18 +03:00
* [How do I import a subpath of a source?](#how-do-i-import-a-subpath-of-a-source)
* [How do I import NixOS modules](#how-do-i-import-nixos-modules)
* [Can I use local packages?](#can-i-use-local-packages)
### 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:
``` shell
$ 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
```
2. add the path to the above file to `/etc/nix/nix.conf`:
```
netrc-file = /PATH/TO/.netrc
```
3. set `GITHUB_TOKEN` env var when calling `niv add`
```
GITHUB_TOKEN=$YOUR_GITHUB_TOKEN niv add ...
```
### How do I import and use the content of a source?
The way to import a source depend mainly on the content targetted by this
source. A source could be a file, a repository with no knowledge of nix
or a repository already in the nix ecosystem.
#### 1. Direct import of a nix based source
In the case of a nix based source, you'll often find a `default.nix` at the
root. Let's take this repository as example. We can add it to our `sources.json`
with the following command.
``` shell
$ niv add nmattia/niv
```
We can now import niv to use it a nix expression, e.g.:
``` nix
{ sources ? import nix/sources.nix }:
let niv = import sources.niv {};
in { inherit niv; } # A glorious expression using the reference to niv
```
#### 2. Import of a nix based source via an overlay
Rather than use the resulting derivation directly, you can add it to your custom
nixpkgs via the overlay system.
``` nix
{ sources ? import nix/sources.nix}:
let overlay = _: pkgs: {
niv = (import sources.niv {}).niv;
};
nixpkgs = import sources.nixpkgs { overlays = [ overlay ]; config = {}; };
in { inherit (nixpkgs) niv; } # A glorious expression where nixpkgs.niv is referenced
```
#### 3. Reference to the source's files in the nix store
You can also reference a simple file, a folder or a repo without nix knowledge
with niv. In these cases, you can use the source in your nix expression without
importing it.
The following exemple will compile gnu hello while using this technique to retrieve
the source. First, we need to add the new source.
``` shell
$ niv add hello-src -v 2.10 -t 'https://ftp.gnu.org/gnu/hello/hello-<version>.tar.gz'
```
Then, we can use it inside a nix expression.
``` nix
{ sources ? import nix/sources.nix }:
let hello_src = sources.hello-src;
nixpkgs = import sources.nixpkgs {};
in nixpkgs.stdenv.mkDerivation {
pname = "hello";
version = "custom";
src = hello_src;
}
```
:warning: If you have problems, consider using the outPath of the source
(e.g. `sources.hello-src.outPath`) instead of the source directly. See
[this issue](https://github.com/nmattia/niv/issues/325) for more details.
2020-07-23 16:55:40 +03:00
### How do I import a subpath of a source?
In order to use the directory `dir` of a `my-package`, use the following
pattern:
``` nix
let
sources = import ./nix/sources.nix;
in sources.my-package + "/dir"
```
2020-07-24 13:24:20 +03:00
in this example, `sources.my-package` becomes `my-package`'s root directory, and `+ "/dir"` appends the
2020-07-23 16:55:40 +03:00
subdirectory.
2020-07-24 13:24:20 +03:00
2020-08-13 16:35:18 +03:00
### How do I import NixOS modules?
2020-07-24 13:24:20 +03:00
After the package containing the modules has been `niv add`ed, importing the
modules is straightforward:
``` nix
let
sources = import ./nix/sources.nix;
in {
imports = [ (sources.package + "/path/to/module") ];
}
```
2020-08-13 16:35:18 +03:00
### 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`.