# Documentation of how to set up a Nix channel?

**URL:** https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781
**Category:** Help
**Created:** [February 6, 2020, 10:53am UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781 "2020-02-06T10:53:43Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![magthe](https://discourse.nixos.org/user_avatar/discourse.nixos.org/magthe/32/1885_2.png) [@magthe](https://discourse.nixos.org/u/magthe)
#### Post date: [February 6, 2020, 10:53am UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/1 "2020-02-06T10:53:43Z")

</div>

I’d like to set up a (private) channel for the tools we use at work. Is there some documentation for how to achieve this?

In the end I’d like to tell my co-workers to run

```auto
$ nix-channel --add <url> work-tools
$ nix-env --install a-work-tool

```

in order to have `a-work-tool` available to them (or something almost that simple). Ideally `nix-channel --update` and `nix-env --upgrade` should work too.

Are there any resources explaining how to do this somewhere?

---

<div class="post-metadata">

### Author: ![efx](https://discourse.nixos.org/user_avatar/discourse.nixos.org/efx/32/7925_2.png) [@efx](https://discourse.nixos.org/u/efx)
#### Post date: [February 6, 2020, 3:12pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/2 "2020-02-06T15:12:36Z")

</div>

I do a similar thing to point at nixpkg’s master.  
I’ve found the manual’s introduction helpful to give an overview: [Introduction - Nix Reference Manual](https://nixos.org/nix/manual/#ch-basic-package-mgmt)  
It links to the “Channels” [chapter](https://nixos.org/nix/manual/#sec-channels) which has more details as well.

---

<div class="post-metadata">

### Author: ![magthe](https://discourse.nixos.org/user_avatar/discourse.nixos.org/magthe/32/1885_2.png) [@magthe](https://discourse.nixos.org/u/magthe)
#### Post date: [February 6, 2020, 10:27pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/3 "2020-02-06T22:27:04Z")

</div>

Isn’t that documentation on how to _consume_ a channel?

I’m looking to _produce_ a channel.

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [February 7, 2020, 9:03am UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/4 "2020-02-07T09:03:26Z")

</div>

A channel is just a folder (or a tarball) with a `default.nix` that returns an attrset.

---

<div class="post-metadata">

### Author: ![magthe](https://discourse.nixos.org/user_avatar/discourse.nixos.org/magthe/32/1885_2.png) [@magthe](https://discourse.nixos.org/u/magthe)
#### Post date: [February 7, 2020, 9:07am UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/5 "2020-02-07T09:07:47Z")

</div>

OK, please bear with a beginner though.

- How do I create the folder/tarball? I’m guessing it has to contain both a `default.nix` and the pre-built binaries, right?
- Do you know of a (small) example I can take a look at? (Looking at `nixpkgs` itself is a bit overwhelming.)

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [February 7, 2020, 10:24am UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/6 "2020-02-07T10:24:32Z")

</div>

@rycee s `home-manager` is distributed over a channel.

The GitHub repository lives at [GitHub - nix-community/home-manager: Manage a user environment using Nix [maintainer=@rycee]](https://github.com/rycee/home-manager).

It contains a `default.nix` at its root. As GitHub provides a tarball for each commit, as well as the “tip” of each branch, and each tag, you can use that tarball as the channel URL.

It as far as I remember the pattern for the name is `https://github.com/${user}/${project}/archive/${ref}.tar.gz`, where `${ref}` is commit SHA, branch or tag.

---

<div class="post-metadata">

### Author: ![magthe](https://discourse.nixos.org/user_avatar/discourse.nixos.org/magthe/32/1885_2.png) [@magthe](https://discourse.nixos.org/u/magthe)
#### Post date: [February 7, 2020, 12:20pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/7 "2020-02-07T12:20:15Z")

</div>

Ah, thanks. It looks simple enough, though I’m not sure where/how pre-built binaries are provided.

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [February 7, 2020, 12:23pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/8 "2020-02-07T12:23:01Z")

</div>

You can use [cachix](https://cachix.org/) to provide them. Or set up a [hydra](https://nixos.org/hydra/) instance, that can be used as a binary cache as well as far as I understand.

Or you just don’t provide a binary cache and let your users build locally.

---

<div class="post-metadata">

### Author: ![tokudan](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/t/dc4da7/32.png) [@tokudan](https://discourse.nixos.org/u/tokudan)
#### Post date: [February 7, 2020, 12:38pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/9 "2020-02-07T12:38:49Z")

</div>

Pre-built binaries are not linked to a channel at all.  
The only very weak link is that [hydra.nixos.org](http://hydra.nixos.org) builds binaries and pushes them to [cache.nixos.org](http://cache.nixos.org) and when that’s done, it updates the channel.

You can provide a binary cache without a channel.  
You can provide a channel without a binary cache.

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [February 7, 2020, 12:59pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/10 "2020-02-07T12:59:02Z")

</div>

Some documentation about setting up a self hosted binary cache can be found in the wiki:

[https://nixos.wiki/wiki/Binary\_Cache](https://nixos.wiki/wiki/Binary_Cache)

---

<div class="post-metadata">

### Author: ![magthe](https://discourse.nixos.org/user_avatar/discourse.nixos.org/magthe/32/1885_2.png) [@magthe](https://discourse.nixos.org/u/magthe)
#### Post date: [February 7, 2020, 11:34pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/11 "2020-02-07T23:34:07Z")

</div>

All right, so a channel is just an expression and caching pre-built binaries is orthogonal to that.

However, when I’ve added my channel (a tar ball served up from GitHub), and I then add a cachix cache for the pre-built binaries I seem to _always build anyways_. I was hoping I’d hit the cache and skip the build, why have a cache otherwise?

When looking at the build output I get different checksums when building (using `nix-build` in git workspace with the `default.nix`) than when installing (using `nix-env -i my-tool`), despite the Nix expression being identical i both cases (i.e. identical `default.nix`). What could cause this unexpected behaviour?

---

<div class="post-metadata">

### Author: ![tomberek](https://discourse.nixos.org/user_avatar/discourse.nixos.org/tomberek/32/283_2.png) [@tomberek](https://discourse.nixos.org/u/tomberek)
#### Post date: [February 8, 2020, 12:31am UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/12 "2020-02-08T00:31:47Z")

</div>

A common issue can be failing to pin the nixpkgs version. Specifically nix-env uses a different source than nix-build under some configurations. This is the difference between ~/.nix-defexpr and NIX\_PATH and channels and root channels and probably a few other settings. A pinned nixpkgs (niv is a good tool for this and possibly the new flakes construct) can help. Once everything is pinned right, builds should give you cache hits.

Other tools that may help, nix-diff and “nix why-depends”

---

<div class="post-metadata">

### Author: ![efx](https://discourse.nixos.org/user_avatar/discourse.nixos.org/efx/32/7925_2.png) [@efx](https://discourse.nixos.org/u/efx)
#### Post date: [February 11, 2020, 3:39pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/13 "2020-02-11T15:39:14Z")

</div>

> [@magthe](#):
>
> Isn’t that documentation on how to _consume_ a channel?
> 
> I’m looking to _produce_ a channel.

Good point! I’m sorry; I missed part of your original need. The main documentation provides some help but I’ve also gotten stuck trying to build my own channel.

---

<div class="post-metadata">

### Author: ![domenkozar](https://discourse.nixos.org/user_avatar/discourse.nixos.org/domenkozar/32/38357_2.png) [@domenkozar](https://discourse.nixos.org/u/domenkozar)
#### Post date: [February 11, 2020, 4:14pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/14 "2020-02-11T16:14:20Z")

</div>

There’s a tool called nix-diff that will tell you the difference between the two.

---

<div class="post-metadata">

### Author: ![magthe](https://discourse.nixos.org/user_avatar/discourse.nixos.org/magthe/32/1885_2.png) [@magthe](https://discourse.nixos.org/u/magthe)
#### Post date: [February 18, 2020, 4:57am UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/15 "2020-02-18T04:57:01Z")

</div>

@tomberek I’ll have to look into those tools, but I don’t think that pinning really explains the cache misses since what I do is:

1. Run `nix-env -i pkg` produces hash `abc`.
2. Push build with hash `abc` to cachix. (I can’t really inspect what’s in cachix, but the used space increases.)
3. Uninstall `pkg`.
4. Run `nix-env -i pkg` again to install.

At 4 I expect the cache to be used, but instead it builds, and this build has the exact same hash (`abc`) as the previous build.

I’ll try to put together a small example.

---

<div class="post-metadata">

### Author: ![magthe](https://discourse.nixos.org/user_avatar/discourse.nixos.org/magthe/32/1885_2.png) [@magthe](https://discourse.nixos.org/u/magthe)
#### Post date: [February 18, 2020, 3:12pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/16 "2020-02-18T15:12:01Z")

</div>

So, here’s a tiny example: [https://github.com/magthe/nix-channel-app](https://github.com/magthe/nix-channel-app)

If I build it 5 times I get

```auto
$ for i in $(seq 5); do nix-build 2> /dev/null ; done <<<
/nix/store/1nbj7d1sh2q8fcrq18n104i3al85132p-nix-channel-app-0.0.1
/nix/store/bc0cnpakr7iqdgxmrvc7a85d53wrrck1-nix-channel-app-0.0.1
/nix/store/0n6ijph1w4q897g7qjv4wgim7ym84kqz-nix-channel-app-0.0.1
/nix/store/0qsz01ixa7kljf0rxrfp2s2apfji3vl5-nix-channel-app-0.0.1
/nix/store/fpzsb595q0pkycil85nvg3f1fmlgfbk5-nix-channel-app-0.0.1

```

When running `nix-diff` on two of the derivations I’m told that

```auto
- 1s95ggixwbnmb5vnqqsc46s221xj5p8c-nix-channel-app-0.0.1.drv:{out}
+ 1c41qn761fhm9kwjh5arnkn20wcp40nm-nix-channel-app-0.0.1.drv:{out}
• The set of input sources do not match:
    - /nix/store/sx4al9mnidsj379w2mlxw91ghcywxfic-nix-0
    + /nix/store/f2bqsww4i751irz5kibnm20r7n9j4lzd-nix-0
• The environments do not match:
    src=''
        /nix/store/→f2bq→s←x←→ww→4←al9mn←i←dsj3←7←9w2mlxw9←→5→1←ghcywxf←i←c←→rz5kibnm20r7n9j4lzd→-nix-0
    ''

```

Clearly _nothing_ relevant has changed between these builds, so why would the environments (and thus the hashes) differ?

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [February 18, 2020, 4:37pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/17 "2020-02-18T16:37:54Z")

</div>

Doing it that way will include the `result` link in your next buildinput.

If you remove it after build, you’ll see the same output:

```auto
$ for i in $(seq 5); do nix-build 2> /dev/null; rm ./result ; done
/nix/store/cr12612ri4sf54qk42676m810i0bqvkh-nix-channel-app-0.0.1
/nix/store/cr12612ri4sf54qk42676m810i0bqvkh-nix-channel-app-0.0.1
/nix/store/cr12612ri4sf54qk42676m810i0bqvkh-nix-channel-app-0.0.1
/nix/store/cr12612ri4sf54qk42676m810i0bqvkh-nix-channel-app-0.0.1
/nix/store/cr12612ri4sf54qk42676m810i0bqvkh-nix-channel-app-0.0.1

```

There is also a flag to `nix-build` that will not even create the `result` link, but I can’t remember which one it was… I think `--no-result-link` or something like that.

---

<div class="post-metadata">

### Author: ![tomberek](https://discourse.nixos.org/user_avatar/discourse.nixos.org/tomberek/32/283_2.png) [@tomberek](https://discourse.nixos.org/u/tomberek)
#### Post date: [February 19, 2020, 1:20am UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/18 "2020-02-19T01:20:31Z")

</div>

As Noobz pointed out, it seems your source is changing.

> <https://github.com/magthe/nix-channel-app/blob/6e0c4e3d4dd6271c226a8a2f0005ca625e53cacd/default.nix#L12>

There are some functions that filter out gitignore and result links: [https://github.com/NixOS/nixpkgs/blob/e2b4abe3c8f2e09adfc6a52007841d1b96c89371/lib/sources.nix#L36](https://github.com/NixOS/nixpkgs/blob/e2b4abe3c8f2e09adfc6a52007841d1b96c89371/lib/sources.nix#L36)

---

<div class="post-metadata">

### Author: ![magthe](https://discourse.nixos.org/user_avatar/discourse.nixos.org/magthe/32/1885_2.png) [@magthe](https://discourse.nixos.org/u/magthe)
#### Post date: [February 19, 2020, 1:15pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/19 "2020-02-19T13:15:58Z")

</div>

Thanks @NobbZ and @tomberek!

The change in [this commit](https://github.com/magthe/nix-channel-app/commit/cd905f1c269e0cef74c5d5963fa3bd5510986e89) makes the build behave as expected:

```auto
for i in $(seq 5); do nix-build 2> /dev/null ; done
/nix/store/nf608j8d0jpgs7ds1wralmn72shs5jja-nix-channel-app-0.0.1
/nix/store/nf608j8d0jpgs7ds1wralmn72shs5jja-nix-channel-app-0.0.1
/nix/store/nf608j8d0jpgs7ds1wralmn72shs5jja-nix-channel-app-0.0.1
/nix/store/nf608j8d0jpgs7ds1wralmn72shs5jja-nix-channel-app-0.0.1
/nix/store/nf608j8d0jpgs7ds1wralmn72shs5jja-nix-channel-app-0.0.1

```

Now on to the next issue, how do I get the built version “into a channel”?

I’ve built it twice on Travis, and the second [build on Travis](https://travis-ci.com/magthe/nix-channel-app/builds/149632135) ends with:

```auto
these paths will be fetched (0.18 MiB download, 0.67 MiB unpacked):

  /nix/store/64ji2j8160idj14vxm1vfw1ykm0ds0r8-nix-channel-app-0.0.1

copying path '/nix/store/64ji2j8160idj14vxm1vfw1ykm0ds0r8-nix-channel-app-0.0.1' from 'https://magthe-temp.cachix.org'...

All done.

```

So I think that means the cache _does_ contains a built version.

I’ve added the following line to `.nix-channels`:

```auto
https://github.com/magthe/nix-channel-app/archive/master.tar.gz nix-channel-app

```

and updated channels:

```auto
$ nix-channel --update
these derivations will be built:
  /nix/store/xr34rr965qp63fsrmy40b4np3dvgi8yx-nix-channel-app.drv
building '/nix/store/xr34rr965qp63fsrmy40b4np3dvgi8yx-nix-channel-app.drv'...
unpacking channels...
created 3 symlinks in user environment

```

However, when I try to install the package, using `nix-env -i nix-channel-app` I end up building the app. The build output (truncated):

```auto
nix-env -i nix-channel-app
unpacking 'https://github.com/NixOS/nixpkgs-channels/archive/0c960262d159d3a884dadc3d4e4b131557dad116.tar.gz'...
copying path '/nix/store/2l5bkv1s788nfzzghwf9ckkc6j0zy75k-busybox-1.31.1-x86_64-unknown-linux-musl' from 'https://cache.nixos.org'...
copying path '/nix/store/jc2jzf3ww11696jrp6l20crbzdnj7x5g-apr-1.7.0' from 'https://cache.nixos.org'...
copying path '/nix/store/1z8wd00d9px0n20jzys93rdpafrndqij-aws-c-common-0.3.11' from 'https://cache.nixos.org'...

....

installing
Installing executable ze-app in /nix/store/64ji2j8160idj14vxm1vfw1ykm0ds0r8-nix-channel-app-0.0.1/bin
Warning: The directory
/nix/store/64ji2j8160idj14vxm1vfw1ykm0ds0r8-nix-channel-app-0.0.1/bin is not
in the system search path.
post-installation fixup
shrinking RPATHs of ELF executables and libraries in /nix/store/64ji2j8160idj14vxm1vfw1ykm0ds0r8-nix-channel-app-0.0.1
shrinking /nix/store/64ji2j8160idj14vxm1vfw1ykm0ds0r8-nix-channel-app-0.0.1/bin/ze-app
strip is /nix/store/7bhi29ainf5rjrk7k7wyhndyskzyhsxh-binutils-2.31.1/bin/strip
stripping (with command strip and flags -S) in /nix/store/64ji2j8160idj14vxm1vfw1ykm0ds0r8-nix-channel-app-0.0.1/bin
patching script interpreter paths in /nix/store/64ji2j8160idj14vxm1vfw1ykm0ds0r8-nix-channel-app-0.0.1
checking for references to /build/ in /nix/store/64ji2j8160idj14vxm1vfw1ykm0ds0r8-nix-channel-app-0.0.1...
building '/nix/store/kmhhapfv7p8rdwqvjjqinyfrf0fx1w5a-user-environment.drv'...
created 23 symlinks in user environment

```

It certainly looks like the built binary is identical to the cached version, the same hash, but why isn’t it just pulled out of the cache when installing?

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [February 19, 2020, 1:24pm UTC](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781/20 "2020-02-19T13:24:01Z")

</div>

Have you enabled the cachix on that host?

[Next page](https://discourse.nixos.org/t/documentation-of-how-to-set-up-a-nix-channel/5781.md?page=2)
