Snippets: Difference between revisions
imported>Matthiasbeyer Create page |
imported>Samueldr m toc |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
__TOC__ | |||
This page contains some nix script snippets which might be useful. | This page contains some nix script snippets which might be useful. | ||
== Adding compiler flags to a package == | == Adding compiler flags to a package == | ||
<syntaxHighlight lang=nix> | |||
optimizeWithFlag = pkg: flag: | |||
pkg.overrideAttrs (attrs: { | |||
NIX_CFLAGS_COMPILE = (attrs.NIX_CFLAGS_COMPILE or "") + " ${flag}"; | |||
}); | |||
</syntaxHighlight> | |||
This function can further be used to define the following helpers: | This function can further be used to define the following helpers: | ||
<syntaxHighlight lang=nix> | |||
optimizeWithFlags = pkg: flags: pkgs.lib.foldl' (pkg: flag: optimizeWithFlag pkg flag) pkg flags; | |||
optimizeForThisHost = pkg: optimizeWithFlags pkg [ "-O3" "-march=native" "-fPIC" ]; | |||
withDebuggingCompiled = pkg: optimizeWithFlag pkg "-DDEBUG"; | |||
</syntaxHighlight> | |||
== Overriding callPackage for finding updated upstream packages in own overrides == | == Overriding callPackage for finding updated upstream packages in own overrides == | ||
With this override for the `callPackage` function, one can automatically detect if the upstream package set has a newer version of an locally overridden package: | With this override for the `callPackage` function, one can automatically detect if the upstream package set has a newer version of an locally overridden package: | ||
<syntaxHighlight lang=nix> | |||
callPackage = path: args: let | |||
override = super.callPackage path args; | |||
upstream = optionalAttrs (override ? "name") | |||
(super.${(parseDrvName override.name).name} or {}); | |||
in if upstream ? "name" && | |||
override ? "name" && | |||
compareVersions upstream.name override.name != -1 | |||
then | |||
trace | |||
"Upstream `${upstream.name}' gets overridden by `${override.name}'." | |||
override | |||
else override; | |||
</syntaxHighlight> |
Latest revision as of 19:16, 7 June 2018
This page contains some nix script snippets which might be useful.
Adding compiler flags to a package
optimizeWithFlag = pkg: flag:
pkg.overrideAttrs (attrs: {
NIX_CFLAGS_COMPILE = (attrs.NIX_CFLAGS_COMPILE or "") + " ${flag}";
});
This function can further be used to define the following helpers:
optimizeWithFlags = pkg: flags: pkgs.lib.foldl' (pkg: flag: optimizeWithFlag pkg flag) pkg flags;
optimizeForThisHost = pkg: optimizeWithFlags pkg [ "-O3" "-march=native" "-fPIC" ];
withDebuggingCompiled = pkg: optimizeWithFlag pkg "-DDEBUG";
Overriding callPackage for finding updated upstream packages in own overrides
With this override for the `callPackage` function, one can automatically detect if the upstream package set has a newer version of an locally overridden package:
callPackage = path: args: let
override = super.callPackage path args;
upstream = optionalAttrs (override ? "name")
(super.${(parseDrvName override.name).name} or {});
in if upstream ? "name" &&
override ? "name" &&
compareVersions upstream.name override.name != -1
then
trace
"Upstream `${upstream.name}' gets overridden by `${override.name}'."
override
else override;