• Stars
    star
    93
  • Rank 359,819 (Top 8 %)
  • Language
    OCaml
  • License
    MIT License
  • Created about 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate nix expressions from opam packages

opam2nix

opam2nix, as its name suggests, takes in opam inputs and generates nix expressions. There are many something2nix tools for various languages, this is the OCaml one.

Note on previous versions:

opam2nix version 0.x was active from ~2015-2019. Version 1.0 introduced a simpler but very different usage. In particular the opam2nix-packages repository was only necessary prior to 1.0, so its documentation still describes that version.

Examples

The easiest way to get started is to check out the examples/ directory. It's got small, working examples that you can probably adapt to your own use very easily.

Getting it

opam2nix is not yet part of nixpkgs, so the easiest way to use it is to make a simple file which imports it:

$ cat opam2nix.nix
import (builtins.fetchTarball "https://github.com/timbertson/opam2nix/archive/v1.tar.gz")

This will always build the latest version, cached based on your nix settings. If you prefer to specify an exact version, you can use fetchFromGitHub, or use a tool like nix-wrangle.

Using it

opam2nix has two interfaces. The first one is the commandline interface, which you'll use to resolve your dependencies into a concrete package set from the requirements in your opam file (my-package.opam for example):

$ "$(nix-build --no-out-link ./opam2nix.nix)/bin/opam2nix" resolve --ocaml-version 4.08.1 ./my-package.opam

This selects a specific version of all transitive dependencies using the latest version of the opam repository by default, although you can specify a specific revision (with --repo-commit=COMMIT / $OPAM_REPO_COMMIT=COMMIT), or skip updating by passing --repo-commit=HEAD.

Secondly, you'll need to use the nix API to actually build the generated package set:

$ cat default.nix
with import <nixpkgs> {};
let
  ocaml = ocaml-ng.ocamlPackages_4_08.ocaml;
  opam2nix = import ./opam2nix.nix {};
  selection = opam2nix.build {
    inherit ocaml;
    selection = ./opam-selection.nix;
    src = ./.;
  };
in
selection.my-package

That's it! Whenever you change your dependencies you'll need to re-generate opam-selection.nix, otherwise you can just use nix-build, nix-shell etc.

API

The opam2nix nix derivation exposes these functions (as passthru attributes):

  • build: build a package set, and return the full selections (an attrset with all opam package names as keys, and derivations as values)
  • buildInputs: as above, but returns an array of derivations (i.e. the attrValues)

Both of these functions accept a single attrset argument, containing:

  • ocaml: required, the version must match the version specified when creating opam-dependencies.nix
  • src: if building a package that doesn't live in the official opam-repository (called "direct" packages by opam2nix), you must provide src. This can either be:
    • a single source value, in which case it's used by all (typically one) direct packages
    • an attrset with a key for each direct package, for when you're building multiple direct packages with distinct sources
  • override: optional, an attrset to supply overrides to selected packages. This can contain as many keys as you like, overrides will only apply to packages that are being built. Override values are described below.

Additionally, there's a third resolve function. It takes two arguments:

  • an attributeset which may contain the same arguments passed to build / buildInputs. Only the ocaml and selection attributes are required / used, the other attributes are ignored so that you can reuse the same attrset.
  • an array of arguments to be passed to opam2nix resolve. The --dest and --ocaml-version arguments are provided for you, so you don't need to specify those.

This does not produce a derivation, instead it produces a shell environment. If you expose this as e.g. an update attribute in your default.nix, you can use it like so:

nix-shell -A update default.nix

This will run opam2nix resolve and then exit, and since it's in a shell it is allowed to do impure things like fetching items from the internet, and writing to your local selections file.

This is just a convenience, but in certain situations it becomes quite useful. In particular when you need to pass in the paths to many opam files which come from nix derivations (e.g. the result of fetchFromGitHub). You can do this by nix-build-ing each one individually and passing them in, but using the resolve function lets you pass these dependencies in as simple expressions and have nix build everything in a single execution.

Overrides

The overrides value is applied to pkgs.callPackage, so it can be either a path or a function. The function can require named arguments from pkgs, plus the additional properties selection and opam2nix. All of these are optional, if your overrides object requires no dependencies it can simply be a function accepting no named arguments (i.e. {}: ...).

The return value of the overrides function must be an attrset, with keys matching opam package names. Excess names (i.e. packages which aren't being built) are allowed, and ignored.

Each value in the returned attrset must be a function accepting a single argument, the base derivation for this opam package. By convention, this is named super. When invoked, this function must return a new derivation, typically by invoking super.overrideAttrs.

An example override argument looks like this:

{ pkgs, selection }: {
	nocrypto = super: super.overrideAttrs (attrs: {
		buildPhase = "export OCAMLRUNPARAM=b; " + attrs.buildPhase;
		hardeningDisable = [ "stackprotector" ];
		propagatedBuildInputs = attrs.propagatedBuildInputs ++ [ selection.ocamlfind pkgs.libffi ];
	});
}

(nocrypto doesn't actually need ocamlfind and libffi added to its build inputs, this is just for illustrative purposes).

How do opam depexts work?

For depexts of os-distribution = "nixos", those will become mandatory (and they're resolved as attributes on whatever pkgs is in use).

If a package has depexts but none of them are for nixos, they'll all become optional depexts - if there is a nixpkgs attribute matching that name it'll be used, otherwise no dep is added.

More Repositories

1

python-readability

[abandoned] python port of arc90's readability bookmarklet
Python
527
star
2

shellshape

tiling window manager extension for gnome-shell
TypeScript
397
star
3

slinger

gnome-shell extension for throwing windows around efficiently
TypeScript
135
star
4

gnome-shell-impatience

speed up gnome-shell animations
JavaScript
78
star
5

gnome-shell-scroll-workspaces

Switch workspaces by scrolling in the top panel
Python
71
star
6

runix

Nix
59
star
7

autonose

autotest-like runner for nosetests
Python
58
star
8

pea

[abandoned] Tiny BDD framework for python
Python
58
star
9

gup

A better make, inspired by djb's redo.
Python
50
star
10

piep

Bringing the power of python to stream editing
Python
48
star
11

nix-wrangle

Wrangle your nix dependencies
Haskell
40
star
12

edit-server

an edit server compatible with TextAid and "Edit with Emacs" chrome extensions
Python
32
star
13

opam2nix-packages

nix expressions for the official opam repository, using opam2nix
Nix
27
star
14

supergenpass

[abandoned] reimplementation of supergenpass algorithm in python, with OS integration
Python
24
star
15

mocktest

powerful mock, expectation and unit test helpers for python
Python
22
star
16

nix-pin

nixpkgs development utility
Python
20
star
17

coffee-spec

[abandoned] spec runner for coffee-script
CoffeeScript
18
star
18

unfluff

[abandoned] statistical HTML content extraction in python
Python
18
star
19

dhall-render

Render multiple files from dhall expressions
Ruby
18
star
20

termstyle

a dirt-simple terminal-colour library for python
Python
15
star
21

stereoscoper

Bulk process stereoscopic (3d) images
Python
10
star
22

fetlock

Convert any lockfile to a nix expression
Rust
9
star
23

pyperclip

improved fork of coffeeghost's `pyperclip` module
Python
9
star
24

nix-update-source

Fetch and automatically update nix derivation sources
Python
9
star
25

gsel

Python
8
star
26

vdoml

virtual DOM + OCaml
OCaml
8
star
27

node-seccomp

C++
7
star
28

foperator

Functional k8s operator framework, in scala
Scala
7
star
29

app-customisations

Mostly dotfiles and whatnot.
Python
6
star
30

py-dataflow

[abandoned] a simple dataflow library for python
Python
5
star
31

vim-watch

run stuff whenever vim saves a file
Python
5
star
32

paragram

an erlang-style concurrency framework for python
Python
5
star
33

repr.js

better stringification for javascript
JavaScript
5
star
34

netproxrc

netrc-enabled https proxy for nix
Go
5
star
35

passe

SuperGenPass-compatible password utility
OCaml
5
star
36

appengine-auth

[abandoned] Google App Engine authorisation for non-web based python applications
Python
4
star
37

vim-stratifiedjs

vim support for sjs
Vim Script
4
star
38

python-irank

rate your music with whatever criteria you like
Python
4
star
39

vim-background-make

[abandoned] a vim plugin to run :make in the background, unobtrusively.
Vim Script
4
star
40

pjkit

[abandoned] python-javascript bridge for webkit-gtk
Python
4
star
41

filepp

[abandoned] mirror of http://freecode.com/projects/filepp with minor changes for ZeroInstall
Perl
4
star
42

pagefeed-android

[abandoned] android app for pagefeed.appspot.com
Scala
3
star
43

pagefeed

[abandoned] the code behind pagefeed.appspot.com
Python
3
star
44

jquery-navim

[abandoned] vim-style keyboard navigation for page elements
JavaScript
3
star
45

bash-cached-completions

a configurable mecahnism for generating (and caching) bash completion words for arbitrary commands
Python
3
star
46

autorelease-tagger-action

Generate & tag versions based on git history
JavaScript
3
star
47

chored

Chores, sorted.
TypeScript
3
star
48

daglink

manage your symlinks
Python
3
star
49

zeroinstall-plugin-manager

plugin manager <runner> for zero-install applications
Python
3
star
50

0env

run a program or shell in the context of a ZeroInstall feed
Python
3
star
51

obligate.js

browser-side module implementation with Zero Install integration
Python
3
star
52

music-import

Pedantically set music filename, metadata and volume normalisation
Python
2
star
53

js-bakery

[abandoned]
JavaScript
2
star
54

0path

[abandoned] path manipulation for testing 0install packages
Python
2
star
55

net-sandbox

[abandoned] sandbox a process inside a new network namespace
Python
2
star
56

capsul

A minimal, type-safe alternative to (some of) akka.
Scala
2
star
57

htmlview

view html files (quickly)
Perl
2
star
58

simple_notify

[abandoned] a python inotify wrapper that's trivial to use
Python
2
star
59

savemytext.appspot.com

[abandoned] exactly what it says on the tin
JavaScript
2
star
60

self-update-action

Generic github action for self-updating codebases
TypeScript
2
star
61

simple-daemon

[abandoned] a simple wrapper around start-stop-daemon to do the right thing most of the time
Python
2
star
62

csharp-quote-argv

C#
2
star
63

version

view (and modify) your project version in multiple commonly-used places
Python
2
star
64

nix-pin-example-sed

A sample repository for demonstrating nix-pin
C
1
star
65

savemytext

[abandoned] programs / plugins to use with savemytext.appspot.com
Python
1
star
66

sbt-strict-scope

Scala
1
star
67

0local

make a local implementation of a 0install feed
Python
1
star
68

eog-rate

Set ratings and tags in Eye of Gnome
Python
1
star
69

mountmon

[abandoned] run a command whenever you attach your kindle via USB
1
star
70

0template-packages

zero install package-implementation feed generator
Python
1
star
71

indent-finder

Fork of http://www.freehackers.org/Indent_Finder
C
1
star
72

dconf-user-overrides

manage your important dconf overrides as text files
Python
1
star
73

0release

[abandoned] 0release (a fork of http://repo.or.cz/w/0release.git)
Python
1
star
74

snip.hs

manage text snippets on the command line
Haskell
1
star
75

0find

[abandoned] 0install utility to find an implementation directory
Python
1
star
76

tail-logs

tail multiple logs (with tmux)
Python
1
star
77

jenkins-indicator

[abandoned] Jenkins Gnome Applet
Python
1
star
78

bash-escape

escape strings for use in shell scripting
Python
1
star
79

zeroinstall-ocaml-0compile

[abandoned] 0compile feed for 0install's ocaml parts (and dependencies)
Python
1
star
80

0downstream

create & maintain zeroinstall feeds for upstream projects
Python
1
star
81

auditspec

Scala
1
star
82

0compile-cabal-build

[abandoned] cabal-build wrapper for use with 0compile
Python
1
star
83

nosetests-runner

[abandoned] nosetests runner using 0install for plugin resolution
Python
1
star
84

backpressure-sensor

Measure backpressure in reactive streams pipelines
Scala
1
star
85

iysr

simple service monitor for systemd
Rust
1
star
86

0readme

populate a zero install feed with README contents
Python
1
star
87

vim-autotab

[abandoned] pretend everyone else agrees with your tab-loving ways
Vim Script
1
star
88

dumbattr

resiliant xattr-based metadata
Python
1
star
89

zero2pypi

create a pypi package from a zero install feed
Python
1
star
90

pycasa

[abandoned] read picasa metadata
Python
1
star
91

importwatcher

[abandoned] module import watcher for python
Python
1
star
92

indicate-task

create a notification applet for the duration of a process (task)
Python
1
star
93

dhall-ci

Reusable CI abstractions with dhall
Dhall
1
star
94

mapnix

Simple NixOS deployment
Python
1
star
95

status-check

Dirt simple notifications when periodic tasks fail to run
OCaml
1
star
96

node-smock

straightforward mocking for commonJS runtimes
JavaScript
1
star
97

mkzero

zero-install feed publishing utility
Python
1
star
98

gedit-plugins

[abandoned] gedit plugins made or modified by me
Python
1
star