From NixOS Wiki
Jump to: navigation, search
m (whitespace cleanup)
m (do not use pkgconfig alias)
Line 2: Line 2:
 
== I installed a library but my compiler is not finding it. Why? ==
 
== I installed a library but my compiler is not finding it. Why? ==
  
With nix, only ''applications'' should be installed into profiles. Libraries are used using nix-shell. If you want to compile a piece of software that requires zlib and uses pkg-config to discover it, run <code>nix-shell -p gcc pkgconfig zlib</code> to get into a shell with the appropriate environment variables set. In there, a configure script will work as expected.
+
With nix, only ''applications'' should be installed into profiles. Libraries are used using nix-shell. If you want to compile a piece of software that requires zlib and uses pkg-config to discover it, run <code>nix-shell -p gcc pkg-config zlib</code> to get into a shell with the appropriate environment variables set. In there, a configure script will work as expected.
  
 
This applies to other language environments too. In some cases the expressions to use are a bit different, e.g. because the interpreter needs to be wrapped to have some additional environment variables passed to it. The manual has [https://nixos.org/nixpkgs/manual/#chap-language-support a section] on the subject.
 
This applies to other language environments too. In some cases the expressions to use are a bit different, e.g. because the interpreter needs to be wrapped to have some additional environment variables passed to it. The manual has [https://nixos.org/nixpkgs/manual/#chap-language-support a section] on the subject.
Line 12: Line 12:
 
stdenv.mkDerivation {
 
stdenv.mkDerivation {
 
     name = "dev-environment"; # Probably put a more meaningful name here
 
     name = "dev-environment"; # Probably put a more meaningful name here
     buildInputs = [ pkgconfig zlib ];
+
     buildInputs = [ pkg-config zlib ];
 
}</syntaxhighlight>
 
}</syntaxhighlight>
 
=== Why does it work like that? ===
 
=== Why does it work like that? ===
  
 
This helps ensure purity of builds: on other distributions, the result of building a piece of software may depend on which other software you have installed. Nix attempts to avoid this to the greatest degree possible, which allows builds of a piece of software to be identical (in the ideal case) no matter where they're built, by requiring all dependencies to be declared.</onlyinclude>
 
This helps ensure purity of builds: on other distributions, the result of building a piece of software may depend on which other software you have installed. Nix attempts to avoid this to the greatest degree possible, which allows builds of a piece of software to be identical (in the ideal case) no matter where they're built, by requiring all dependencies to be declared.</onlyinclude>

Revision as of 19:46, 16 June 2020

I installed a library but my compiler is not finding it. Why?

With nix, only applications should be installed into profiles. Libraries are used using nix-shell. If you want to compile a piece of software that requires zlib and uses pkg-config to discover it, run nix-shell -p gcc pkg-config zlib to get into a shell with the appropriate environment variables set. In there, a configure script will work as expected.

This applies to other language environments too. In some cases the expressions to use are a bit different, e.g. because the interpreter needs to be wrapped to have some additional environment variables passed to it. The manual has a section on the subject.

If you have a lot of dependencies, you may want to write a nix expression that includes your dependencies so that you can simply use nix-shell rather than writing out each dependency every time or keeping your development environment in your shell history. A minimal example looks like this:

# default.nix
with import <nixpkgs> {};
stdenv.mkDerivation {
    name = "dev-environment"; # Probably put a more meaningful name here
    buildInputs = [ pkg-config zlib ];
}

Why does it work like that?

This helps ensure purity of builds: on other distributions, the result of building a piece of software may depend on which other software you have installed. Nix attempts to avoid this to the greatest degree possible, which allows builds of a piece of software to be identical (in the ideal case) no matter where they're built, by requiring all dependencies to be declared.