Using Clang instead of GCC: Difference between revisions

imported>Symphorien
explain how to get a specific version of clang
DHCP (talk | contribs)
m style fixes
 
(6 intermediate revisions by 3 users not shown)
Line 10: Line 10:
Depending on the case you may want to set this value in different location, and using different mechanism.
Depending on the case you may want to set this value in different location, and using different mechanism.


Note you may get errors like <code>fatal error: ... file not found</code> on standard library <code>#include</code> directives, because of this bug https://github.com/NixOS/nixpkgs/issues/150655


== Globally, in a package repository tree ==
== Globally, in a package repository tree ==


If you have a set of packages in a repository tree, you can set the
If you have a set of packages in a repository tree, you can set the
<code>stdenv</code> value in the scope where the <code>callPackage</code> are
<code>stdenv</code> value in the scope where the <code>callPackage</code> are
called. Be carefull '''all the packages present in the scope will be built with Clang'''  
called. Be carefull '''all the packages present in the scope will be built with Clang'''
because the <code>callPackage</code> that resolves the package function
because the <code>callPackage</code> that resolves the package function
inputs will use the  <code>pkgs.clangStdenv</code> for all packages.
inputs will use the  <code>pkgs.clangStdenv</code> for all packages.
Line 22: Line 22:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
rec {
rec {
    stdenv = pkgs.clangStdenv;
  stdenv = pkgs.clangStdenv;
    foo = callPackage ./foo { };
  foo = callPackage ./foo { };
    bar = callPackage ./bar { };
  bar = callPackage ./bar { };
}
}
</syntaxhighlight>
or import nixpkgs with replaceStdenv.
<syntaxhighlight lang="nix">
import <nixpkgs> { config = { replaceStdenv = ({ pkgs }: pkgs.clangStdenv); }; }
</syntaxhighlight>
</syntaxhighlight>


Line 38: Line 44:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
rec {
rec {
    foo = callPackage ./foo { stdenv = pkgs.clangStdenv; };
  foo = callPackage ./foo { stdenv = pkgs.clangStdenv; };
    bar = callPackage ./bar { };
  bar = callPackage ./bar { };
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 47: Line 53:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
rec {
rec {
    foo_gcc = callPackage ./foo { };
  foo_gcc = callPackage ./foo { };
    foo_clang = callPackage ./foo { stdenv = pkgs.clangStdenv; };
  foo_clang = callPackage ./foo { stdenv = pkgs.clangStdenv; };
    bar = callPackage ./bar { };
  bar = callPackage ./bar { };
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 58: Line 64:
Directly inline with CLI just do:
Directly inline with CLI just do:


<syntaxhighlight lang="bash">
<syntaxhighlight lang=console>
nix-build -E "with import <nixpkgs> {}; pkgs.hello.override{ stdenv = pkgs.clangStdenv; }"
$ nix-build -E "with import <nixpkgs> {}; pkgs.hello.override{ stdenv = pkgs.clangStdenv; }"
</syntaxhighlight>
</syntaxhighlight>


or, if you want a shell for development:
or, if you want a shell for development:


<syntaxhighlight lang="bash">
<syntaxhighlight lang=console>
nix-shell -E "with import <nixpkgs> {}; pkgs.hello.override{ stdenv = pkgs.clangStdenv; }"
$ nix-shell -E "with import <nixpkgs> {}; pkgs.hello.override{ stdenv = pkgs.clangStdenv; }"
</syntaxhighlight>
</syntaxhighlight>


== Using an external override definition ==
== Using an external override definition ==


<syntaxhighlight lang="nix">
{{file|./hello_with_clan.nix|nix|3=
# in file ./hello_with_clan.nix
with import <nixpkgs> {};
with import <nixpkgs> {};
hello.override {
hello.override {
    # use Clang instead of GCC
  # use Clang instead of GCC
    stdenv = pkgs.clangStdenv;
  stdenv = pkgs.clangStdenv;
}
}
</syntaxhighlight>
}}


<syntaxhighlight lang="bash">
<syntaxhighlight lang=console>
nix-build ./hello_with_clan.nix
$ nix-build ./hello_with_clan.nix
</syntaxhighlight>
</syntaxhighlight>


== With nix-shell ==
== With nix-shell ==
To use clang in nix-shell instead of gcc:
To use clang in nix-shell instead of gcc:
<syntaxhighlight lang="nix">
{{file|./shell.nix|nix|3=
# in file ./shell.nix
with import <nixpkgs> {};
with import <nixpkgs> {};
clangStdenv.mkDerivation {
clangStdenv.mkDerivation {
Line 92: Line 96:
   buildInputs = [ /* add libraries here */ ];
   buildInputs = [ /* add libraries here */ ];
}
}
</syntaxhighlight>
}}
 
== See also ==


* [[C#Use_a_different_compiler_version|C # Use a different compiler version]]


[[Category:Nixpkgs]]
[[Category:Nixpkgs]]