Snippets: Difference between revisions

From NixOS Wiki
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:
optimizeWithFlag = pkg: flag:
      pkg.overrideAttrs (attrs: {
  pkg.overrideAttrs (attrs: {
        NIX_CFLAGS_COMPILE = (attrs.NIX_CFLAGS_COMPILE or "") + " ${flag}";
    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:


    optimizeWithFlags = pkg: flags: pkgs.lib.foldl' (pkg: flag: optimizeWithFlag pkg flag) pkg flags;
<syntaxHighlight lang=nix>
 
optimizeWithFlags = pkg: flags: pkgs.lib.foldl' (pkg: flag: optimizeWithFlag pkg flag) pkg flags;
    optimizeForThisHost = pkg: optimizeWithFlags pkg [ "-O3" "-march=native" "-fPIC" ];
optimizeForThisHost = pkg: optimizeWithFlags pkg [ "-O3" "-march=native" "-fPIC" ];
 
withDebuggingCompiled = pkg: optimizeWithFlag pkg "-DDEBUG";
    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
callPackage = path: args: let
        override =  super.callPackage path args;
    override =  super.callPackage path args;
        upstream = optionalAttrs (override ? "name")
    upstream = optionalAttrs (override ? "name")
          (super.${(parseDrvName override.name).name} or {});
      (super.${(parseDrvName override.name).name} or {});
      in if upstream ? "name" &&
  in if upstream ? "name" &&
            override ? "name" &&
        override ? "name" &&
            compareVersions upstream.name override.name != -1
        compareVersions upstream.name override.name != -1
        then
    then
          trace
      trace
            "Upstream `${upstream.name}' gets overridden by `${override.name}'."
        "Upstream `${upstream.name}' gets overridden by `${override.name}'."
            override
        override
        else override;
    else override;
    # Credit to makefu
</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;