Nixpkgs/Create and debug packages: Difference between revisions

imported>Jmarmstrong1207
Change method to run specific phase. runPhase was added and easier to use
Das-g (talk | contribs)
m Using nix-shell for package development: Improve grammar in note about preConfigurePhase
 
(5 intermediate revisions by 4 users not shown)
Line 37: Line 37:
# 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 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.
# 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</code>. Iterate on tweaking the derivation and rebuilding until it succeeds.
# For large projects with long compile times, you can use <code>nix-shell</code> instead to [https://ryantm.github.io/nixpkgs/stdenv/stdenv/#sec-building-stdenv-package-in-nix-shell run the individual phases].
# For large projects with long compile times, you can use <code>nix-shell</code> instead to [https://nixos.org/manual/nixpkgs/stable/#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 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].
# Read on below for further steps.
# Read on below for further steps.
Line 197: Line 197:
You would have seen the dependencies downloading, but the ''bc-build'' directory remains empty. The build system would next invoke a builder with some arguments. You can obtain the exact name of the builder (usually '''bash''') and the arguments '''args''' of the builder (typically a shell script) by checking the corresponding value in:
You would have seen the dependencies downloading, but the ''bc-build'' directory remains empty. The build system would next invoke a builder with some arguments. You can obtain the exact name of the builder (usually '''bash''') and the arguments '''args''' of the builder (typically a shell script) by checking the corresponding value in:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ nix show-derivation $(nix-instantiate .)
$ nix derivation show $(nix-instantiate .)
</syntaxhighlight>
</syntaxhighlight>


Line 249: Line 249:
</syntaxhighlight>
</syntaxhighlight>


{{Note|you do not need to run $preConfigurePhase explicitly as it is run, when running configurePhase already.}}
{{Note|You do not need to run $preConfigurePhase explicitly, as it will already be run implicitly when running configurePhase.}}


To list all functions which are declared in '''set''':
To list all functions which are declared in '''set''':
Line 379: Line 379:
Your program to test the library:
Your program to test the library:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{ pkgs ? import <nixpkgs> {} }:
{
with pkgs;
  pkgs ? import <nixpkgs> {
let yourLibraryName = callPackage /path/to/library/derivation.nix {};
    overlays = [
in
      (final: prev: {
stdenv.mkDerivation {
        my-library = prev.callPackage ./my-library.nix { };
  nativeBuildInputs = [
      })
     yourLibraryName /* Add here if needed at build time */
    ];
     /* Add other dependencies if needed */
  },
  ]
}:
  buildInputs = [
pkgs.callPackage (
     yourLibraryName /* Add here if needed at runtime */
  {
     /* Add other dependencies if needed */
    stdenv,
  ]
    hello,
   /* Add the other stuff as needed */
    pkg-config,
}
    my-library,
  }:
  stdenv.mkDerivation {
    pname = "something";
    version = "1";
    strictDeps = true;
     # host/target agnostic programs
     depsBuildBuild = [
      hello
    ];
    # compilers & linkers & dependecy finding programs
    nativeBuildInputs = [
      pkg-config
     ];
    # libraries
     buildInputs = [
      my-library
    ];
   }
) { }
</syntaxhighlight>
</syntaxhighlight>


Line 442: Line 461:
</syntaxhighlight>
</syntaxhighlight>


== Formatting Packages with nixpkgs-fmt ==
== Formatting Packages with nixfmt ==
It is "good practice" to format packages in a way that following changed will create as minimal diffs as possible. The formatter [https://github.com/nix-community/nixpkgs-fmt nixpkgs-fmt] can be used for that.
It is "good practice" to format packages in a way that following changed will create as minimal diffs as possible. The formatter [https://github.com/NixOS/nixfmt nixfmt] can be used for that.
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
nixpkgs-fmt path/to/default.nix
nix-shell -p nixfmt-rfc-style --run 'nixfmt path/to/default.nix'
</syntaxhighlight>
</syntaxhighlight>