### Build or Check opam-nix Examples Source: https://github.com/tweag/opam-nix/blob/main/README.md The opam-nix repository includes various examples demonstrating different use cases. You can build individual examples using `nix build` or check all examples simultaneously using `nix flake check`. ```Shell nix build github:tweag/opam-nix#opam-ed ``` ```Shell nix flake check github:tweag/opam-nix ``` -------------------------------- ### Building Opam Project' - All Packages - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This example uses `buildOpamProject'` which is similar to `buildOpamProject` but includes all packages found within the specified project directory in the resulting scope, rather than just the one specified by name. ```Nix (buildOpamProject' { } ./. { }).my-package ``` -------------------------------- ### Building Opam Project - Static Linking - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This example illustrates how to build a package from a local directory using `buildOpamProject` with a specific set of Nix packages (`pkgsStatic`) to achieve static linking for the resulting library or binary. ```Nix (buildOpamProject { pkgs = pkgsStatic; } "my-package" ./. { }).my-package ``` -------------------------------- ### Build Package with queryToScope Defaults - opam-nix - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Example demonstrating the basic usage of `queryToScope` to build a package (`opam-ed`) and its OCaml system dependency from the default opam-repository using the latest available versions. ```Nix (queryToScope { } { opam-ed = "*"; ocaml-system = "*"; }).opam-ed ``` -------------------------------- ### Building Opam Project - With External Repo - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This example demonstrates building a package from a local directory (`./.`) that depends on packages from the standard `opamRepository`. It constructs a repository from the local directory using `makeOpamRepo` and combines it with `opamRepository` before using `queryToScope`. ```Nix let repos = [ (makeOpamRepo ./.) opamRepository ]; scope = queryToScope { inherit repos; } { "my-package" = "*"; }; in scope."my-package" ``` -------------------------------- ### Building Opam Project - Basic - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This example demonstrates the basic usage of `buildOpamProject` to build a package named 'my-package' located in the current directory. It uses default options for repositories, Nixpkgs, overlays, and resolve arguments. ```Nix (buildOpamProject { } "my-package" ./. { }).my-package ``` -------------------------------- ### Build Package with queryToScope Overrides - opam-nix - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Example showing how to use `queryToScope` to build a specific version of a package (`opam-ed 0.3`) and then apply an overlay using `overrideScope` to modify the build attributes of a dependency (`opam-file-format`). ```Nix let scope = queryToScope { } { opam-ed = "0.3"; ocaml-system = "*"; }; overlay = self: super: { opam-file-format = super.opam-file-format.overrideAttrs (oa: { opam__ocaml__native = "true"; }); }; in (scope.overrideScope overlay).opam-ed ``` -------------------------------- ### Build Package with queryToScope Static Pkgs - opam-nix - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Example illustrating how to pass a different set of Nixpkgs (`pkgsStatic`) to `queryToScope` to potentially build packages with static linking enabled, using default settings for the package query. ```Nix let scope = queryToScope { pkgs = pkgsStatic; } { opam-ed = "*"; ocaml-system = "*"; }; in scope.opam-ed ``` -------------------------------- ### Building local package with vendored dependencies and overlay in Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This example demonstrates how to build a local Opam package using opam-nix. It shows how to define repositories, create a query from an opam.export file, include vendored packages, apply a local overlay, and build the final package within the resulting Nix scope. ```Nix let pkgs = import { }; repos = [ (opam-nix.makeOpamRepo ./.) # "Pin" vendored packages inputs.opam-repository ]; export = opam-nix.opamListToQuery (opam-nix.fromOPAM ./opam.export).installed; vendored-packages = { "my-vendored-package" = "local"; "my-other-vendored-package" = "v1.2.3"; "my-package" = "local"; # Note: you can't use "*" here! }; myOverlay = import ./overlay.nix; scope = applyOverlays [ defaultOverlay myOverlay ] (defsToScope pkgs defaultResolveEnv (queryToDefs repos (export // vendored-packages))); in scope.my-package ``` -------------------------------- ### Combine opamImport and opam2nix in Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This example demonstrates how to use `opamImport` to create a package scope from an opam export file and then use `opam2nix` to define a local package within that scope using `callPackage`. It shows a typical pattern for integrating a local OCaml project with dependencies managed by opam-nix. ```Nix let scope = opamImport { } ./opam.export; pkg = opam2nix { src = ./.; name = "my-package"; }; in scope.callPackage pkg {} ``` -------------------------------- ### Evaluate and save Nix expression output (Shell) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This shell command evaluates a Nix expression (in this case, assumed to be the `package-defs` attribute from the previous snippet) to get the path to the generated file and then uses `cat` to save the content of that file to `package-defs.json`. This is necessary because `materializeOpamProject` returns a path, not the content directly. ```Shell cat $(nix eval --raw .#package-defs) > package-defs.json ``` -------------------------------- ### Override opam-nix Package Attributes (Nix) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Demonstrates how to override attributes on an opam-nix package derivation using `overrideAttrs`. This example shows how to set an opam environment variable, noting the required transformation of variable names (replacing `-` and `+` with `_`, `:` with `__`, and prepending `opam__`). ```Nix cmdliner.overrideAttrs (_: { opam__conf_g____installed = "true"; }) ``` -------------------------------- ### opamImport Function Type Signature (Nix) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Describes the type signature of the `opamImport` function, which imports an opam switch and provides a Nix package scope containing the installed packages. It takes optional repository list, Nixpkgs instance, and overlays, returning a Path and a Scope. ```Nix { repos = ?[Repository] ; pkgs = ?Nixpkgs ; overlays = ?[Overlay] } → Path → Scope ``` -------------------------------- ### Initialize opam-nix Project with Templates Source: https://github.com/tweag/opam-nix/blob/main/README.md Use `nix flake init` with different templates provided by opam-nix to quickly set up a Nix flake for an existing opam project. These templates offer various configurations, from a simple package build to multi-package workspaces. ```Shell nix flake init -t github:tweag/opam-nix ``` ```Shell nix flake init -t github:tweag/opam-nix#executable ``` ```Shell nix flake init -t github:tweag/opam-nix#multi-package ``` -------------------------------- ### Building Dune Project - Basic - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This snippet shows how to use `buildDuneProject` to build a local project that uses Dune and may not have a pre-existing opam file. It internally runs `dune build` to generate the necessary opam file before building. ```Nix (buildDuneProject { } "my-package" ./. { }).my-package ``` -------------------------------- ### Building Opam Project - With Tests - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This snippet demonstrates how to build an opam project including its tests by setting the `with-test` flag within the `resolveArgs` attribute passed to `buildOpamProject`. This is useful for ensuring tests pass during the build process. ```Nix (buildOpamProject { resolveArgs.with-test = true; } "my-package" ./. { }).my-package ``` -------------------------------- ### Accessing opam-nix Lib via Flake - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Demonstrates how to access the platform-specific `lib` output of the `opam-nix` flake using `flake-utils`, making the opam-nix functions available in the system-specific scope. ```Nix { inputs.opam-nix.url = "github:tweag/opam-nix"; inputs.flake-utils.url = "github:numtide/flake-utils"; outputs = { self, opam-nix, flake-utils }: flake-utils.lib.eachDefaultSystem (system: with opam-nix.lib.${system}; { defaultPackage = # }); } ``` -------------------------------- ### Use opam-repository Packages via legacyPackages Source: https://github.com/tweag/opam-nix/blob/main/README.md opam-nix exposes all packages from the opam-repository through its `legacyPackages` flake output. This allows building specific package versions or entering a shell with a particular package without managing opam switches. ```Shell nix build 'github:tweag/opam-nix#utop."2.10.0"' ``` ```Shell nix shell github:tweag/opam-nix#camyll.latest ``` -------------------------------- ### Accessing opam-nix Lib via Flake-less Expression - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Shows how to access the platform-specific `lib` output of `opam-nix` in a flake-less Nix expression by fetching the tarball and importing the library for the current system. ```Nix with (import (builtins.fetchTarball "https://github.com/tweag/opam-nix/archive/main.tar.gz")).lib.${builtins.currentSystem}; # example goes here ``` -------------------------------- ### Generate package definitions with materializeOpamProject (Nix) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This Nix code snippet shows how to directly call the `materializeOpamProject` function to generate a path to a file containing materialized package definitions. This is an alternative to using the `opam-nix-gen` script when integrating materialization directly into Nix code. ```Nix # ... package-defs = materializeOpamProject { } "my-package" ./. { }; # ... ``` -------------------------------- ### Add flake.nix to Git Index Source: https://github.com/tweag/opam-nix/blob/main/README.md After initializing a Nix flake, it's important to add the generated `flake.nix` file to your Git index, as Nix flake commands often operate on the contents tracked by Git. ```Shell git add flake.nix ``` -------------------------------- ### Building Opam Project - Specify Compiler - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This snippet shows how to use `buildOpamProject` to build a package while explicitly specifying the 'ocaml-base-compiler' version using the query argument. This forces opam to use a non-system compiler. ```Nix (buildOpamProject { } "my-package" ./. { ocaml-base-compiler = "*"; }).my-package ``` -------------------------------- ### Define opam-nix Query Structure (Nix) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Illustrates the structure of an opam-nix 'Query', which is an attrset mapping package names to desired versions. The version can be a specific string or `"*"` to indicate the latest available version. ```Nix { ${package_name} = package_version : String or "*"; ... } ``` -------------------------------- ### Import materialized definitions into Scope (Nix) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This Nix code snippet demonstrates how to use the `materializedDefsToScope` function to create a package scope from a previously generated `package-defs.json` file. This allows using the materialized definitions without requiring IFD or the opam toolchain during evaluation. ```Nix (materializedDefsToScope { sourceMap.my-package = ./.; } ./package-defs.json).my-package ``` -------------------------------- ### Generate package definitions with opam-nix-gen (Shell) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md This shell command uses the `opam-nix-gen` script to generate a `package-defs.json` file for a specified opam project. The script wraps the underlying Nix materialization functions, providing a convenient command-line interface. ```Shell opam-nix-gen my-package . package-defs.json ``` -------------------------------- ### queryToScope Function Signature - opam-nix - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Provides the type signature for the `queryToScope` function, showing it takes an arguments object (including optional repos, pkgs, overlays, resolveArgs) and a Query, and returns a Scope. ```Nix { ; repos = ?[Repository] ; pkgs = ?Nixpkgs ; overlays = ?[Overlay] ; resolveArgs = ?ResolveArgs } ``` -------------------------------- ### materializeOpamProject' Function Type Signature (Nix) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Describes the type signature of the `materializeOpamProject'` function, similar to `materializeOpamProject` but automatically includes all packages found in the project directory. It takes optional repository list, resolution arguments, a boolean for pinning dependencies, and a regeneration command, resolving a query for a project at a given path and producing a Path to a JSON file. ```Nix materializeOpamProject' : { repos = ?[Repository] ; resolveArgs = ?ResolveArgs ; pinDepends = ?Boolean ; regenCommand = ?[String]} → project : Path → Query → Path ``` -------------------------------- ### materializeOpamProject Function Type Signature (Nix) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Describes the type signature of the `materializeOpamProject` function, a wrapper around `materialize` for opam projects. It takes optional repository list, resolution arguments, a boolean for pinning dependencies, and a regeneration command, resolving a query for a named project at a given path and producing a Path to a JSON file. ```Nix materializeOpamProject : { repos = ?[Repository] ; resolveArgs = ?ResolveArgs ; pinDepends = ?Boolean ; regenCommand = ?[String]} → name : String → project : Path → Query → Path ``` -------------------------------- ### opam2nix Function Type Signature (Nix) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Describes the type signature of the `opam2nix` function, which converts an opam file into a callPackage-able Nix package definition. It requires the source path and can optionally take the opam file path, name, version, and environment resolution arguments, returning dependencies and a Package. ```Nix { src = Path ; opamFile = ?Path ; name = ?String ; version = ?String ; resolveEnv = ?ResolveEnv } → Dependencies → Package ``` -------------------------------- ### materializedDefsToScope Function Type Signature (Nix) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Describes the type signature of the `materializedDefsToScope` function, which converts a JSON file containing materialized package definitions into a Nix package scope. It takes optional Nixpkgs instance and overlays, reads the JSON file from a given Path, and returns a Scope. ```Nix materializedDefsToScope : { pkgs = ?Nixpkgs ; overlays = ?[Overlay] } → Path → Scope ``` -------------------------------- ### materialize Function Type Signature (Nix) Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Describes the type signature of the `materialize` function, used for materializing package definitions to avoid IFD. It takes optional repository list, resolution arguments, and a regeneration command, resolving a query and producing a Path to a JSON file. ```Nix materialize : { repos = ?[Repository] ; resolveArgs = ?ResolveArgs ; regenCommand = ?String} → Query → Path ``` -------------------------------- ### ResolveArgs Type Definition - opam-nix - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Defines the structure of the `ResolveArgs` type, an attribute set containing boolean flags and an optional environment (`env`) to control the opam resolution process within `queryToScope`. ```Nix ResolveArgs : { ; env = ?ResolveEnv ; with-test = ?Bool ; with-doc = ?Bool ; dev = ?Bool ; depopts = ?Bool ; best-effort = ?Bool } ``` -------------------------------- ### Scope Structure Definition - opam-nix - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Defines the structure of a Nixpkgs "scope" or package set within opam-nix, including key functions like `overrideScope` and `callPackage`, and placeholders for packages. This structure represents an opam switch equivalent in Nix. ```Nix { overrideScope = (Scope → Scope → Scope) → Scope ; callPackage = (Dependencies → Package) → Dependencies → Package ; ${package_name} = package : Package; ... } ``` -------------------------------- ### ResolveEnv Type Definition - opam-nix - Nix Source: https://github.com/tweag/opam-nix/blob/main/DOCUMENTATION.md Defines the structure of the `ResolveEnv` type, which is an attribute set mapping variable names to string values, used to pass environment variables to the opam resolver. ```Nix ResolveEnv : { ${var_name} = value : String; ... } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.