Nixpkgs/Create and debug packages: Difference between revisions
imported>Hypnosis2839 m →Packages from source code: minor cleanup, fix command name |
imported>Hypnosis2839 →Packages from source code: Remove references to nix3 CLI, fix instructions for phase-by-phase builds, clean up tone, remove repetitive phrasing |
||
Line 28: | Line 28: | ||
There are different steps here depending on whether you're building from source or packaging an existing binary. There are some common steps too. | There are different steps here depending on whether you're building from source or packaging an existing binary. There are some common steps too. | ||
=== | === Package from source code === | ||
# Read the repo build instructions | # Read the repo build instructions and CI scripts (for example, on GitHub, these are located in <code>.github/workflows</code>). | ||
# Look in nixpkgs for a package with a similar build process to use as reference | # Look in nixpkgs for a package with a similar build process to use as reference. For example, if you're packaging a project written in Go, find a package for an existing Go application. Each language has its own supporting Nix functions and a more or less standard way of dealing with things. For example, [https://nixos.org/manual/nixpkgs/stable/#sec-language-go Go] has <code>buildGoModule</code>. [https://nixos.org/manual/nixpkgs/stable/#rust Rust] has <code>buildRustPackage</code>. [https://nixos.org/manual/nixpkgs/stable/#python Python] has <code>buildPythonApplication</code>. [https://nixos.org/manual/nixpkgs/stable/#node.js Node.js] has <code>node2nix</code>, <code>yarn2nix</code>, etc. There are also specific functions for wrapping e.g. [https://nixos.org/manual/nixpkgs/stable/#sec-language-gnome GNOME] applications (<code>wrapGAppsHook</code>), or [https://nixos.org/manual/nixpkgs/stable/#sec-language-qt Qt] apps (<code>libsForQt5</code>, <code>wrapQtAppsHook</code>). Refer to the [https://nixos.org/manual/nixpkgs/stable/#chap-language-support language support chapter in the nixpkgs manual]. | ||
# If there isn't a specific | # If there isn't a specific builder for the language, use <code>stdenv.mkDerivation</code> directly, which has built-in support for GNU make (and other build systems, provided you add the necessary <code>nativeBuildInputs</code>). | ||
# Figure out at least some dependencies from the project repo | # Figure out at least some dependencies from the project repo. See if they're available in nixpkgs (<code>nix search some-library</code> or <code>nix-locate --top-level lib/somelibrary.so</code>). If any dependency is missing you'll need to package that as well. | ||
# | # Create your derivation in <code>default.nix</code> in some empty local directory. | ||
# At the top of the derivation, temporarily add <code>with import <nixpkgs> {};</code>. For now, don't worry too much about declaring every dependency as a parameter<!-- TODO clarify. "parameter of the default build function"? -->, to save time. | |||
# At the top of | # Build the package with <code>nix-build</code>. Iterate on tweaking the derivation and rebuilding until it succeeds. | ||
# Build the package with <code>nix build | # For large projects with long compile times, you can use [[Development environment with nix-shell|nix-shell]] with <code>nix-shell</code>. Inside the development shell, you can [https://ryantm.github.io/nixpkgs/stdenv/stdenv/#sec-building-stdenv-package-in-nix-shell run the individual phases]. | ||
# | # At this stage, you may encounter some build quirks of the project. Compile-time errors will hopefully explain what you're missing. For example [https://github.com/NixOS/nixpkgs/blob/643ce4bd0f057bc0b90f0faebeb83a3b14f01674/pkgs/tools/package-management/micromamba/default.nix#L6-L10 micromamba needs a specialized build of libsolv]. | ||
# At this stage, you may encounter some build | |||
# Read on below for further steps. | # Read on below for further steps. | ||