mirror of
https://github.com/aviallon/nixos-lib.git
synced 2026-04-05 17:27:50 +00:00
fix(lint): nixfmt the whole tree
This commit is contained in:
parent
643b136863
commit
bf219a30c2
69 changed files with 2605 additions and 1726 deletions
|
|
@ -1,17 +1,25 @@
|
|||
{lib, myLib, ...}:
|
||||
{ lib, myLib, ... }:
|
||||
with lib;
|
||||
rec {
|
||||
mergeAttrsRecursive = a: b: foldAttrs (item: acc:
|
||||
if (isNull acc) then
|
||||
item
|
||||
else if (isList item) then
|
||||
if isList acc then
|
||||
acc ++ item
|
||||
else [ acc ] ++ item
|
||||
else if (isString item) then
|
||||
acc + item
|
||||
else if (isAttrs item) then
|
||||
mergeAttrsRecursive acc item
|
||||
else item
|
||||
) null [ b a ];
|
||||
mergeAttrsRecursive =
|
||||
a: b:
|
||||
foldAttrs
|
||||
(
|
||||
item: acc:
|
||||
if (isNull acc) then
|
||||
item
|
||||
else if (isList item) then
|
||||
if isList acc then acc ++ item else [ acc ] ++ item
|
||||
else if (isString item) then
|
||||
acc + item
|
||||
else if (isAttrs item) then
|
||||
mergeAttrsRecursive acc item
|
||||
else
|
||||
item
|
||||
)
|
||||
null
|
||||
[
|
||||
b
|
||||
a
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,28 @@
|
|||
{lib, myLib}:
|
||||
{ lib, myLib }:
|
||||
with lib;
|
||||
let
|
||||
mkListToString = { sep ? " " }: list: concatStringsSep sep (
|
||||
forEach list (v: toString v)
|
||||
);
|
||||
in rec {
|
||||
mkListToString =
|
||||
{
|
||||
sep ? " ",
|
||||
}:
|
||||
list: concatStringsSep sep (forEach list (v: toString v));
|
||||
in
|
||||
rec {
|
||||
mkValueString =
|
||||
let
|
||||
gen = generators.mkValueStringDefault {};
|
||||
listToString = mkListToString {};
|
||||
in v: if isList v then listToString v
|
||||
else gen v;
|
||||
|
||||
mkKeyValue = { sep }: with generators; toKeyValue {
|
||||
mkKeyValue = mkKeyValueDefault {
|
||||
mkValueString = mkValueString;
|
||||
} sep;
|
||||
};
|
||||
gen = generators.mkValueStringDefault { };
|
||||
listToString = mkListToString { };
|
||||
in
|
||||
v: if isList v then listToString v else gen v;
|
||||
|
||||
mkKeyValue =
|
||||
{ sep }:
|
||||
with generators;
|
||||
toKeyValue {
|
||||
mkKeyValue = mkKeyValueDefault {
|
||||
mkValueString = mkValueString;
|
||||
} sep;
|
||||
};
|
||||
|
||||
toSystemd = mkKeyValue {
|
||||
sep = "=";
|
||||
|
|
|
|||
|
|
@ -1,22 +1,37 @@
|
|||
{ lib, myLib }:
|
||||
with lib;
|
||||
rec {
|
||||
toPretty = depth: x:
|
||||
toPretty =
|
||||
depth: x:
|
||||
# Stolen from: https://github.com/teto/nixpkgs/blob/6f098631f6f06b93c17f49abdf677790e017778d/lib/debug.nix#L109C5-L117C30
|
||||
let
|
||||
snip = v: if isList v then noQuotes "[…]" v
|
||||
else if isAttrs v then noQuotes "{…}" v
|
||||
else v;
|
||||
noQuotes = str: v: { __pretty = const str; val = v; };
|
||||
modify = n: fn: v: if (n == 0) then fn v
|
||||
else if isList v then map (modify (n - 1) fn) v
|
||||
else if isAttrs v then mapAttrs
|
||||
(const (modify (n - 1) fn)) v
|
||||
else v;
|
||||
in lib.generators.toPretty { allowPrettyValues = true; } (modify depth snip x);
|
||||
|
||||
traceValWithPrefix = prefix: value:
|
||||
snip =
|
||||
v:
|
||||
if isList v then
|
||||
noQuotes "[…]" v
|
||||
else if isAttrs v then
|
||||
noQuotes "{…}" v
|
||||
else
|
||||
v;
|
||||
noQuotes = str: v: {
|
||||
__pretty = const str;
|
||||
val = v;
|
||||
};
|
||||
modify =
|
||||
n: fn: v:
|
||||
if (n == 0) then
|
||||
fn v
|
||||
else if isList v then
|
||||
map (modify (n - 1) fn) v
|
||||
else if isAttrs v then
|
||||
mapAttrs (const (modify (n - 1) fn)) v
|
||||
else
|
||||
v;
|
||||
in
|
||||
lib.generators.toPretty { allowPrettyValues = true; } (modify depth snip x);
|
||||
|
||||
traceValWithPrefix =
|
||||
prefix: value:
|
||||
#trace "traceValWithPrefix 'prefix': ${prefix}" value
|
||||
trace "${prefix}: ${toPretty 2 value}" value
|
||||
;
|
||||
trace "${prefix}: ${toPretty 2 value}" value;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,27 @@
|
|||
{ lib ? import <nixpkgs/lib, ... }:
|
||||
{
|
||||
lib ? import < nixpkgs/lib,
|
||||
...
|
||||
}:
|
||||
let
|
||||
myLib = lib.makeExtensible (self: let
|
||||
callLibs = file: import file {
|
||||
inherit lib;
|
||||
myLib = self;
|
||||
};
|
||||
in {
|
||||
math = callLibs ./math.nix;
|
||||
config = callLibs ./config.nix;
|
||||
optimizations = callLibs ./optimizations.nix;
|
||||
attrsets = callLibs ./attrsets.nix;
|
||||
types = callLibs ./types.nix;
|
||||
debug = callLibs ./debug.nix;
|
||||
derivations = callLibs ./derivations.nix;
|
||||
});
|
||||
in myLib
|
||||
myLib = lib.makeExtensible (
|
||||
self:
|
||||
let
|
||||
callLibs =
|
||||
file:
|
||||
import file {
|
||||
inherit lib;
|
||||
myLib = self;
|
||||
};
|
||||
in
|
||||
{
|
||||
math = callLibs ./math.nix;
|
||||
config = callLibs ./config.nix;
|
||||
optimizations = callLibs ./optimizations.nix;
|
||||
attrsets = callLibs ./attrsets.nix;
|
||||
types = callLibs ./types.nix;
|
||||
debug = callLibs ./debug.nix;
|
||||
derivations = callLibs ./derivations.nix;
|
||||
}
|
||||
);
|
||||
in
|
||||
myLib
|
||||
|
|
|
|||
|
|
@ -1,15 +1,13 @@
|
|||
{lib, myLib, ...}:
|
||||
{ lib, myLib, ... }:
|
||||
with lib;
|
||||
rec {
|
||||
isBroken = x:
|
||||
isBroken =
|
||||
x:
|
||||
let
|
||||
tryX = builtins.tryEval x;
|
||||
in
|
||||
if
|
||||
tryX.success && (isDerivation tryX.value)
|
||||
then
|
||||
tryX.value.meta.insecure || tryX.value.meta.broken
|
||||
else
|
||||
true
|
||||
;
|
||||
if tryX.success && (isDerivation tryX.value) then
|
||||
tryX.value.meta.insecure || tryX.value.meta.broken
|
||||
else
|
||||
true;
|
||||
}
|
||||
|
|
|
|||
18
lib/math.nix
18
lib/math.nix
|
|
@ -1,12 +1,16 @@
|
|||
{lib
|
||||
, myLib
|
||||
{
|
||||
lib,
|
||||
myLib,
|
||||
}:
|
||||
|
||||
rec {
|
||||
log2 = let
|
||||
mylog = x: y: if (x >= 2) then mylog (x / 2) (y + 1) else y;
|
||||
in x: mylog x 0;
|
||||
log2 =
|
||||
let
|
||||
mylog = x: y: if (x >= 2) then mylog (x / 2) (y + 1) else y;
|
||||
in
|
||||
x: mylog x 0;
|
||||
|
||||
clamp = min_x: max_x: x: lib.min ( lib.max x min_x ) max_x;
|
||||
clamp =
|
||||
min_x: max_x: x:
|
||||
lib.min (lib.max x min_x) max_x;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,15 +33,20 @@ let
|
|||
"-Wl,-sort-common"
|
||||
"-Wl,--gc-sections"
|
||||
];
|
||||
ltoFlags = { threads ? 1, thin ? false }: [
|
||||
# Fat LTO objects are object files that contain both the intermediate language and the object code. This makes them usable for both LTO linking and normal linking.
|
||||
"-flto=${toString threads}" # Use -flto=auto to use GNU make’s job server, if available, or otherwise fall back to autodetection of the number of CPU threads present in your system.
|
||||
(optionalString (!thin) "-ffat-lto-objects")
|
||||
"-fuse-linker-plugin"
|
||||
ltoFlags =
|
||||
{
|
||||
threads ? 1,
|
||||
thin ? false,
|
||||
}:
|
||||
[
|
||||
# Fat LTO objects are object files that contain both the intermediate language and the object code. This makes them usable for both LTO linking and normal linking.
|
||||
"-flto=${toString threads}" # Use -flto=auto to use GNU make’s job server, if available, or otherwise fall back to autodetection of the number of CPU threads present in your system.
|
||||
(optionalString (!thin) "-ffat-lto-objects")
|
||||
"-fuse-linker-plugin"
|
||||
|
||||
# Stream extra information needed for aggressive devirtualization when running the link-time optimizer in local transformation mode.
|
||||
"-fdevirtualize-at-ltrans"
|
||||
];
|
||||
# Stream extra information needed for aggressive devirtualization when running the link-time optimizer in local transformation mode.
|
||||
"-fdevirtualize-at-ltrans"
|
||||
];
|
||||
expensiveOptimizationFlags = [
|
||||
"-O3"
|
||||
# Perform interprocedural pointer analysis and interprocedural modification and reference analysis. This option can cause excessive memory and compile-time usage on large compilation units.
|
||||
|
|
@ -104,66 +109,62 @@ let
|
|||
"-floop-nest-optimize" # "Calculates a loop structure optimized for data-locality and parallelism."
|
||||
];
|
||||
|
||||
archToX86Level = arch:
|
||||
archToX86Level =
|
||||
arch:
|
||||
let
|
||||
_map = { }
|
||||
_map =
|
||||
{ }
|
||||
// genAttrs [
|
||||
"nehalem"
|
||||
"westmere"
|
||||
"sandybridge"
|
||||
"ivybridge"
|
||||
"silvermont"
|
||||
"goldmont"
|
||||
"goldmont-plus"
|
||||
"tremont"
|
||||
"lujiazui"
|
||||
"btver2" # Jaguar
|
||||
"bdver1" # Bulldozer and Piledriver (AMD FX family)
|
||||
"bdver2" # Piledriver
|
||||
"bdver3" # Steamroller
|
||||
"x86-64-v2"
|
||||
]
|
||||
(name: 2)
|
||||
"nehalem"
|
||||
"westmere"
|
||||
"sandybridge"
|
||||
"ivybridge"
|
||||
"silvermont"
|
||||
"goldmont"
|
||||
"goldmont-plus"
|
||||
"tremont"
|
||||
"lujiazui"
|
||||
"btver2" # Jaguar
|
||||
"bdver1" # Bulldozer and Piledriver (AMD FX family)
|
||||
"bdver2" # Piledriver
|
||||
"bdver3" # Steamroller
|
||||
"x86-64-v2"
|
||||
] (name: 2)
|
||||
// genAttrs [
|
||||
"haswell"
|
||||
"broadwell"
|
||||
"skylake"
|
||||
"alderlake"
|
||||
"bdver4" # Excavator
|
||||
"znver1"
|
||||
"znver2"
|
||||
"znver3"
|
||||
"x86-64-v3"
|
||||
]
|
||||
(name: 3)
|
||||
"haswell"
|
||||
"broadwell"
|
||||
"skylake"
|
||||
"alderlake"
|
||||
"bdver4" # Excavator
|
||||
"znver1"
|
||||
"znver2"
|
||||
"znver3"
|
||||
"x86-64-v3"
|
||||
] (name: 3)
|
||||
// genAttrs [
|
||||
"knl"
|
||||
"knm"
|
||||
"skylake-avx512"
|
||||
"cannonlake"
|
||||
"icelake-client"
|
||||
"icelake-server"
|
||||
"cascadelake"
|
||||
"cooperlake"
|
||||
"tigerlake"
|
||||
"sapphirerapids"
|
||||
"rocketlake"
|
||||
"znver4"
|
||||
"x86-64-v4"
|
||||
]
|
||||
(name: 4)
|
||||
;
|
||||
"knl"
|
||||
"knm"
|
||||
"skylake-avx512"
|
||||
"cannonlake"
|
||||
"icelake-client"
|
||||
"icelake-server"
|
||||
"cascadelake"
|
||||
"cooperlake"
|
||||
"tigerlake"
|
||||
"sapphirerapids"
|
||||
"rocketlake"
|
||||
"znver4"
|
||||
"x86-64-v4"
|
||||
] (name: 4);
|
||||
in
|
||||
if (hasAttr arch _map) then _map.${arch} else 1
|
||||
;
|
||||
if (hasAttr arch _map) then _map.${arch} else 1;
|
||||
|
||||
getARMLevel = arch:
|
||||
if (! isNull arch) then
|
||||
toInt (elemAt (builtins.match "armv([0-9]).+") 0)
|
||||
else null;
|
||||
getARMLevel =
|
||||
arch: if (!isNull arch) then toInt (elemAt (builtins.match "armv([0-9]).+") 0) else null;
|
||||
|
||||
# https://go.dev/doc/install/source#environment
|
||||
getGOARM = armLevel: if (isNull armLevel) || (armLevel < 5) || (armLevel > 7) then null else armLevel;
|
||||
getGOARM =
|
||||
armLevel: if (isNull armLevel) || (armLevel < 5) || (armLevel > 7) then null else armLevel;
|
||||
|
||||
workarounds = {
|
||||
# https://www.intel.com/content/dam/support/us/en/documents/processors/mitigations-jump-conditional-code-erratum.pdf
|
||||
|
|
@ -177,7 +178,8 @@ let
|
|||
};
|
||||
};
|
||||
|
||||
addMarchSpecific = march:
|
||||
addMarchSpecific =
|
||||
march:
|
||||
let
|
||||
_map = {
|
||||
skylake = workarounds.intel-jump-conditional-code;
|
||||
|
|
@ -188,16 +190,22 @@ let
|
|||
in
|
||||
attrByPath [ march ] { } _map;
|
||||
|
||||
|
||||
cacheTuning = { compiler, l1d ? null, l1i ? null, l1Line ? null, lastLevel ? null }:
|
||||
if compiler == "gcc" then [ ]
|
||||
++ optional (! isNull l1d) "--param l1-cache-size=${toString l1d}"
|
||||
++ optional (! isNull l1Line) "--param l1-cache-line-size=${toString l1Line}"
|
||||
++ optional (! isNull lastLevel) "--param l2-cache-size=${toString lastLevel}"
|
||||
cacheTuning =
|
||||
{
|
||||
compiler,
|
||||
l1d ? null,
|
||||
l1i ? null,
|
||||
l1Line ? null,
|
||||
lastLevel ? null,
|
||||
}:
|
||||
if compiler == "gcc" then
|
||||
[ ]
|
||||
++ optional (!isNull l1d) "--param l1-cache-size=${toString l1d}"
|
||||
++ optional (!isNull l1Line) "--param l1-cache-line-size=${toString l1Line}"
|
||||
++ optional (!isNull lastLevel) "--param l2-cache-size=${toString lastLevel}"
|
||||
else
|
||||
[ ];
|
||||
|
||||
|
||||
in
|
||||
rec {
|
||||
|
||||
|
|
@ -209,51 +217,63 @@ rec {
|
|||
"very-unsafe" = 5;
|
||||
};
|
||||
|
||||
addAttrs = pkg: attrs: pkg.overrideAttrs (old:
|
||||
(myLib.attrsets.mergeAttrsRecursive old attrs) // {
|
||||
passthru = (pkg.passthru or {}) // (attrs.passtru or {});
|
||||
}
|
||||
);
|
||||
addAttrs =
|
||||
pkg: attrs:
|
||||
pkg.overrideAttrs (
|
||||
old:
|
||||
(myLib.attrsets.mergeAttrsRecursive old attrs)
|
||||
// {
|
||||
passthru = (pkg.passthru or { }) // (attrs.passtru or { });
|
||||
}
|
||||
);
|
||||
|
||||
optimizePkg = pkg: { level ? "normal"
|
||||
, recursive ? 0
|
||||
, optimizeFlags ? (guessOptimizationFlags pkg)
|
||||
, blacklist ? [ ]
|
||||
, ltoBlacklist ? [ ]
|
||||
, overrideMap ? { }
|
||||
, stdenv ? null
|
||||
, lto ? false
|
||||
, attributes ? null
|
||||
, _depth ? 0
|
||||
, ...
|
||||
}@attrs:
|
||||
optimizePkg =
|
||||
pkg:
|
||||
{
|
||||
level ? "normal",
|
||||
recursive ? 0,
|
||||
optimizeFlags ? (guessOptimizationFlags pkg),
|
||||
blacklist ? [ ],
|
||||
ltoBlacklist ? [ ],
|
||||
overrideMap ? { },
|
||||
stdenv ? null,
|
||||
lto ? false,
|
||||
attributes ? null,
|
||||
_depth ? 0,
|
||||
...
|
||||
}@attrs:
|
||||
if _depth > recursive then
|
||||
pkg # Max depth reached, return un-modified pkg
|
||||
else if isNull pkg then
|
||||
pkg # Pkg is null, ignore
|
||||
else if ! isDerivation pkg then
|
||||
else if !isDerivation pkg then
|
||||
pkg # Pkg is not a derivation, nothing to override/optimize
|
||||
else if (hasAttr "overrideAttrs" pkg) then
|
||||
let
|
||||
_pkgStdenvCC = attrByPath [ "stdenv" "cc" ] null pkg;
|
||||
_ltoBlacklisted = any (p: p == getName pkg) ltoBlacklist;
|
||||
_lto =
|
||||
if (lto && _ltoBlacklisted) then warn "LTO-blacklisted '${getName pkg}'" false
|
||||
else lto;
|
||||
_lto = if (lto && _ltoBlacklisted) then warn "LTO-blacklisted '${getName pkg}'" false else lto;
|
||||
_stdenvCC = if isNull stdenv then _pkgStdenvCC else stdenv.cc;
|
||||
optimizedAttrs = optimizeFlags (attrs // {
|
||||
inherit level;
|
||||
compiler =
|
||||
if isNull _pkgStdenvCC then null
|
||||
else if pkg.stdenv.cc.isGNU then "gcc"
|
||||
else if pkg.stdenv.cc.isClang then "clang"
|
||||
else throw "Unknown compiler '${getName pkg.stdenv.cc}'" null
|
||||
;
|
||||
lto = _lto;
|
||||
stdenvCC = _stdenvCC;
|
||||
});
|
||||
_nativeBuildInputs = filter (p: ! isNull p) (pkg.nativeBuildInputs or [ ]);
|
||||
_nativeBuildInputsOverriden = forEach _nativeBuildInputs (_pkg:
|
||||
optimizedAttrs = optimizeFlags (
|
||||
attrs
|
||||
// {
|
||||
inherit level;
|
||||
compiler =
|
||||
if isNull _pkgStdenvCC then
|
||||
null
|
||||
else if pkg.stdenv.cc.isGNU then
|
||||
"gcc"
|
||||
else if pkg.stdenv.cc.isClang then
|
||||
"clang"
|
||||
else
|
||||
throw "Unknown compiler '${getName pkg.stdenv.cc}'" null;
|
||||
lto = _lto;
|
||||
stdenvCC = _stdenvCC;
|
||||
}
|
||||
);
|
||||
_nativeBuildInputs = filter (p: !isNull p) (pkg.nativeBuildInputs or [ ]);
|
||||
_nativeBuildInputsOverriden = forEach _nativeBuildInputs (
|
||||
_pkg:
|
||||
let
|
||||
_pkgName = myGetName _pkg;
|
||||
hasOverride = any (n: n == _pkgName) (attrNames overrideMap);
|
||||
|
|
@ -265,201 +285,224 @@ rec {
|
|||
_pkg
|
||||
);
|
||||
|
||||
_buildInputs = filter (p: (! isNull p) && (isDerivation p)) (pkg.buildInputs or [ ]);
|
||||
_buildInputsOverriden = forEach _buildInputs (_pkg:
|
||||
_buildInputs = filter (p: (!isNull p) && (isDerivation p)) (pkg.buildInputs or [ ]);
|
||||
_buildInputsOverriden = forEach _buildInputs (
|
||||
_pkg:
|
||||
if (any (n: n == myGetName _pkg) blacklist) then
|
||||
warn "Skipping blacklisted '${myGetName _pkg}'" _pkg
|
||||
else
|
||||
optimizePkg _pkg (attrs // {
|
||||
inherit level recursive blacklist optimizeFlags stdenv;
|
||||
parallelize = null;
|
||||
_depth = _depth + 1;
|
||||
})
|
||||
optimizePkg _pkg (
|
||||
attrs
|
||||
// {
|
||||
inherit
|
||||
level
|
||||
recursive
|
||||
blacklist
|
||||
optimizeFlags
|
||||
stdenv
|
||||
;
|
||||
parallelize = null;
|
||||
_depth = _depth + 1;
|
||||
}
|
||||
)
|
||||
);
|
||||
_pkgStdenvOverridable = attrByPath [ "override" "__functionArgs" "stdenv" ] null pkg;
|
||||
_pkgWithStdenv =
|
||||
if (isNull _pkgStdenvOverridable) || (isNull stdenv)
|
||||
then pkg
|
||||
else warn "Replacing stdenv for '${myGetName pkg}'" (pkg.override { inherit stdenv; });
|
||||
if (isNull _pkgStdenvOverridable) || (isNull stdenv) then
|
||||
pkg
|
||||
else
|
||||
warn "Replacing stdenv for '${myGetName pkg}'" (pkg.override { inherit stdenv; });
|
||||
|
||||
_pkg = _pkgWithStdenv.overrideAttrs (old:
|
||||
_pkg = _pkgWithStdenv.overrideAttrs (
|
||||
old:
|
||||
{
|
||||
buildInputs = _buildInputsOverriden;
|
||||
nativeBuildInputs = _nativeBuildInputsOverriden;
|
||||
|
||||
}
|
||||
// optionalAttrs (! isNull _stdenvCC && _stdenvCC.isGNU) ({
|
||||
// optionalAttrs (!isNull _stdenvCC && _stdenvCC.isGNU) ({
|
||||
AR = "${_stdenvCC.cc}/bin/gcc-ar";
|
||||
RANLIB = "${_stdenvCC.cc}/bin/gcc-ranlib";
|
||||
NM = "${_stdenvCC.cc}/bin/gcc-nm";
|
||||
})
|
||||
# Fix issue when CFLAGS is a string
|
||||
// optionalAttrs (hasAttr "CFLAGS" old) {
|
||||
CFLAGS = if (! isList old.CFLAGS) then [ old.CFLAGS ] else old.CFLAGS;
|
||||
CFLAGS = if (!isList old.CFLAGS) then [ old.CFLAGS ] else old.CFLAGS;
|
||||
}
|
||||
);
|
||||
_pkgOptimized = addAttrs _pkg optimizedAttrs;
|
||||
_pkgFinal =
|
||||
if isAttrs attributes then
|
||||
addAttrs _pkgOptimized (traceVal attributes)
|
||||
else
|
||||
_pkgOptimized
|
||||
;
|
||||
if isAttrs attributes then addAttrs _pkgOptimized (traceVal attributes) else _pkgOptimized;
|
||||
in
|
||||
trace "Optimized ${myGetName pkg} with overrideAttrs at level '${level}' (depth: ${toString _depth}, lto: ${if lto then "true" else "false"})" _pkgFinal
|
||||
else if (hasAttr "name" pkg) then
|
||||
warn "Can't optimize ${myGetName pkg} (depth: ${toString _depth})" pkg
|
||||
else
|
||||
throw "Not a pkg: ${builtins.toJSON pkg} (depth: ${toString _depth})" pkg
|
||||
;
|
||||
throw "Not a pkg: ${builtins.toJSON pkg} (depth: ${toString _depth})" pkg;
|
||||
|
||||
myGetName = pkg:
|
||||
if isDerivation pkg
|
||||
then getName pkg
|
||||
else null;
|
||||
myGetName = pkg: if isDerivation pkg then getName pkg else null;
|
||||
#else warn "getName input is not a derivation: '${toString pkg}'" null;
|
||||
|
||||
guessOptimizationFlags = pkg: { ... }@attrs: makeOptimizationFlags ({
|
||||
rust = any (p: (myGetName p) == "rustc") pkg.nativeBuildInputs;
|
||||
cmake = any (p: (myGetName p) == "cmake") pkg.nativeBuildInputs;
|
||||
go = any (p: (myGetName p) == "go") pkg.nativeBuildInputs;
|
||||
ninja = any (p: (myGetName p) == "ninja") pkg.nativeBuildInputs;
|
||||
autotools = any (p: (myGetName p) == "autoreconf-hook") pkg.nativeBuildInputs;
|
||||
} // attrs);
|
||||
guessOptimizationFlags =
|
||||
pkg:
|
||||
{ ... }@attrs:
|
||||
makeOptimizationFlags (
|
||||
{
|
||||
rust = any (p: (myGetName p) == "rustc") pkg.nativeBuildInputs;
|
||||
cmake = any (p: (myGetName p) == "cmake") pkg.nativeBuildInputs;
|
||||
go = any (p: (myGetName p) == "go") pkg.nativeBuildInputs;
|
||||
ninja = any (p: (myGetName p) == "ninja") pkg.nativeBuildInputs;
|
||||
autotools = any (p: (myGetName p) == "autoreconf-hook") pkg.nativeBuildInputs;
|
||||
}
|
||||
// attrs
|
||||
);
|
||||
|
||||
makeOptimizationFlags =
|
||||
{ level ? "normal"
|
||||
, extraCFlags ? null
|
||||
, lto ? false
|
||||
, parallelize ? null
|
||||
, cpuArch ? null
|
||||
, cpuTune ? null
|
||||
, ISA ? "amd64"
|
||||
, armLevel ? (getARMLevel cpuArch)
|
||||
, x86Level ? (archToX86Level cpuArch)
|
||||
, check ? false
|
||||
, compiler ? "gcc"
|
||||
, stdenvCC ? null
|
||||
, cpuCores ? 4
|
||||
, go ? false
|
||||
, rust ? false
|
||||
, cmake ? false
|
||||
, ninja ? false
|
||||
, autotools ? false
|
||||
, l1LineCache ? null
|
||||
, l1iCache ? null
|
||||
, l1dCache ? null
|
||||
, lastLevelCache ? null
|
||||
, ...
|
||||
{
|
||||
level ? "normal",
|
||||
extraCFlags ? null,
|
||||
lto ? false,
|
||||
parallelize ? null,
|
||||
cpuArch ? null,
|
||||
cpuTune ? null,
|
||||
ISA ? "amd64",
|
||||
armLevel ? (getARMLevel cpuArch),
|
||||
x86Level ? (archToX86Level cpuArch),
|
||||
check ? false,
|
||||
compiler ? "gcc",
|
||||
stdenvCC ? null,
|
||||
cpuCores ? 4,
|
||||
go ? false,
|
||||
rust ? false,
|
||||
cmake ? false,
|
||||
ninja ? false,
|
||||
autotools ? false,
|
||||
l1LineCache ? null,
|
||||
l1iCache ? null,
|
||||
l1dCache ? null,
|
||||
lastLevelCache ? null,
|
||||
...
|
||||
}:
|
||||
let
|
||||
levelN = levelNames.${level};
|
||||
march =
|
||||
if (! isNull cpuArch) then cpuArch
|
||||
else if (! isNull cpuTune) then cpuTune
|
||||
else "generic";
|
||||
if (!isNull cpuArch) then
|
||||
cpuArch
|
||||
else if (!isNull cpuTune) then
|
||||
cpuTune
|
||||
else
|
||||
"generic";
|
||||
uarchTune =
|
||||
if (! isNull cpuTune) then cpuTune
|
||||
else if (! isNull cpuArch) then cpuArch
|
||||
else "generic";
|
||||
in myLib.debug.traceValWithPrefix "optimizations" (foldl' myLib.attrsets.mergeAttrsRecursive {} [
|
||||
(rec {
|
||||
CFLAGS = unique
|
||||
([ ]
|
||||
++ requiredFlags
|
||||
++ optionals (compiler == "clang") clangSpecificFlags
|
||||
++ optionals (levelN >= 1) genericCompileFlags
|
||||
++ optionals (levelN >= 2) expensiveOptimizationFlags
|
||||
++ optionals (levelN >= 3) moderatelyUnsafeOptimizationFlags
|
||||
++ optionals (levelN >= 4) unsafeOptimizationFlags
|
||||
++ optionals (levelN >= 5) veryUnsafeOptimizationFlags
|
||||
++ optionals lto (ltoFlags { threads = myLib.math.log2 cpuCores; })
|
||||
++ optionals (! isNull parallelize) (automaticallyParallelizeFlags parallelize)
|
||||
++ optionals (! isNull extraCFlags) extraCFlags
|
||||
++ optionals (! isNull cpuArch) [ "-march=${cpuArch}" ]
|
||||
++ optionals (! isNull cpuTune) [ "-mtune=${uarchTune}" ]
|
||||
++ cacheTuning {
|
||||
inherit compiler;
|
||||
l1Line = l1LineCache;
|
||||
l1i = l1iCache;
|
||||
l1d = l1dCache;
|
||||
lastLevel = lastLevelCache;
|
||||
});
|
||||
CXXFLAGS = CFLAGS;
|
||||
CPPFLAGS = []
|
||||
++ optionals (levelN >= 1) genericPreprocessorFlags;
|
||||
LDFLAGS = []
|
||||
++ optionals (levelN >= 3) genericLinkerFlags;
|
||||
if (!isNull cpuTune) then
|
||||
cpuTune
|
||||
else if (!isNull cpuArch) then
|
||||
cpuArch
|
||||
else
|
||||
"generic";
|
||||
in
|
||||
myLib.debug.traceValWithPrefix "optimizations" (
|
||||
foldl' myLib.attrsets.mergeAttrsRecursive { } [
|
||||
(rec {
|
||||
CFLAGS = unique (
|
||||
[ ]
|
||||
++ requiredFlags
|
||||
++ optionals (compiler == "clang") clangSpecificFlags
|
||||
++ optionals (levelN >= 1) genericCompileFlags
|
||||
++ optionals (levelN >= 2) expensiveOptimizationFlags
|
||||
++ optionals (levelN >= 3) moderatelyUnsafeOptimizationFlags
|
||||
++ optionals (levelN >= 4) unsafeOptimizationFlags
|
||||
++ optionals (levelN >= 5) veryUnsafeOptimizationFlags
|
||||
++ optionals lto (ltoFlags {
|
||||
threads = myLib.math.log2 cpuCores;
|
||||
})
|
||||
++ optionals (!isNull parallelize) (automaticallyParallelizeFlags parallelize)
|
||||
++ optionals (!isNull extraCFlags) extraCFlags
|
||||
++ optionals (!isNull cpuArch) [ "-march=${cpuArch}" ]
|
||||
++ optionals (!isNull cpuTune) [ "-mtune=${uarchTune}" ]
|
||||
++ cacheTuning {
|
||||
inherit compiler;
|
||||
l1Line = l1LineCache;
|
||||
l1i = l1iCache;
|
||||
l1d = l1dCache;
|
||||
lastLevel = lastLevelCache;
|
||||
}
|
||||
);
|
||||
CXXFLAGS = CFLAGS;
|
||||
CPPFLAGS = [ ] ++ optionals (levelN >= 1) genericPreprocessorFlags;
|
||||
LDFLAGS = [ ] ++ optionals (levelN >= 3) genericLinkerFlags;
|
||||
|
||||
preConfigure = ''
|
||||
|
||||
_maxLoad=$(($NIX_BUILD_CORES * 2))
|
||||
makeFlagsArray+=("-l''${_maxLoad}")
|
||||
|
||||
'';
|
||||
})
|
||||
(optionalAttrs autotools {
|
||||
preConfigure = ''
|
||||
preConfigure = ''
|
||||
|
||||
configureFlagsArray+=(
|
||||
"CFLAGS=$CFLAGS"
|
||||
"CXXFLAGS=$CXXFLAGS"
|
||||
)
|
||||
|
||||
'';
|
||||
})
|
||||
(optionalAttrs cmake {
|
||||
preConfigure = ''
|
||||
|
||||
cmakeFlagsArray+=(
|
||||
"-DCMAKE_CXX_FLAGS=$CXXFLAGS"
|
||||
"-DCMAKE_C_FLAGS=$CFLAGS"
|
||||
${optionalString lto ''
|
||||
"-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=true"
|
||||
''}
|
||||
)
|
||||
|
||||
''
|
||||
;
|
||||
})
|
||||
(optionalAttrs ninja {
|
||||
preConfigure = ''
|
||||
|
||||
_maxLoad=$(($NIX_BUILD_CORES * 2))
|
||||
ninjaFlagsArray+=("-l''${_maxLoad}")
|
||||
|
||||
'';
|
||||
})
|
||||
(optionalAttrs rust {
|
||||
RUSTFLAGS = [ ]
|
||||
++ optionals (levelN >= 2) [ "-C opt-level=3" ]
|
||||
++ optionals lto [ "-C lto=fat" "-C embed-bitcode=on" ]
|
||||
++ optionals (! isNull cpuArch) [ "-C target-cpu=${cpuArch}" ]
|
||||
#++ [ "-C embed-bitcode=off" "-C lto=off" ] # Not needed since rust 1.45
|
||||
#++ optionals lto [ "-Clinker-plugin-lto" "-Clto" ]
|
||||
;
|
||||
})
|
||||
(optionalAttrs (!check) {
|
||||
doCheck = false;
|
||||
doInstallCheck = false;
|
||||
})
|
||||
(optionalAttrs (go && ISA == "amd64") {
|
||||
GOAMD64 = "v${toString x86Level}";
|
||||
})
|
||||
(optionalAttrs (go && ISA == "arm") {
|
||||
GOARM = toString (getGOARM armLevel);
|
||||
})
|
||||
(optionalAttrs (go && ISA == "i686") {
|
||||
GO386 = "sse2";
|
||||
})
|
||||
(optionalAttrs go {
|
||||
GCCGO = "gccgo";
|
||||
CGO_CFLAGS_ALLOW = "-f.*";
|
||||
CGO_CXXFLAGS_ALLOW = "-f.*";
|
||||
CGO_CPPFLAGS_ALLOW = "-D.*";
|
||||
CGO_LDFLAGS_ALLOW = "-Wl.*";
|
||||
})
|
||||
(addMarchSpecific march)
|
||||
])
|
||||
;
|
||||
_maxLoad=$(($NIX_BUILD_CORES * 2))
|
||||
makeFlagsArray+=("-l''${_maxLoad}")
|
||||
|
||||
'';
|
||||
})
|
||||
(optionalAttrs autotools {
|
||||
preConfigure = ''
|
||||
|
||||
configureFlagsArray+=(
|
||||
"CFLAGS=$CFLAGS"
|
||||
"CXXFLAGS=$CXXFLAGS"
|
||||
)
|
||||
|
||||
'';
|
||||
})
|
||||
(optionalAttrs cmake {
|
||||
preConfigure = ''
|
||||
|
||||
cmakeFlagsArray+=(
|
||||
"-DCMAKE_CXX_FLAGS=$CXXFLAGS"
|
||||
"-DCMAKE_C_FLAGS=$CFLAGS"
|
||||
${optionalString lto ''
|
||||
"-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=true"
|
||||
''}
|
||||
)
|
||||
|
||||
'';
|
||||
})
|
||||
(optionalAttrs ninja {
|
||||
preConfigure = ''
|
||||
|
||||
_maxLoad=$(($NIX_BUILD_CORES * 2))
|
||||
ninjaFlagsArray+=("-l''${_maxLoad}")
|
||||
|
||||
'';
|
||||
})
|
||||
(optionalAttrs rust {
|
||||
RUSTFLAGS =
|
||||
[ ]
|
||||
++ optionals (levelN >= 2) [ "-C opt-level=3" ]
|
||||
++ optionals lto [
|
||||
"-C lto=fat"
|
||||
"-C embed-bitcode=on"
|
||||
]
|
||||
++ optionals (!isNull cpuArch) [ "-C target-cpu=${cpuArch}" ]
|
||||
#++ [ "-C embed-bitcode=off" "-C lto=off" ] # Not needed since rust 1.45
|
||||
#++ optionals lto [ "-Clinker-plugin-lto" "-Clto" ]
|
||||
;
|
||||
})
|
||||
(optionalAttrs (!check) {
|
||||
doCheck = false;
|
||||
doInstallCheck = false;
|
||||
})
|
||||
(optionalAttrs (go && ISA == "amd64") {
|
||||
GOAMD64 = "v${toString x86Level}";
|
||||
})
|
||||
(optionalAttrs (go && ISA == "arm") {
|
||||
GOARM = toString (getGOARM armLevel);
|
||||
})
|
||||
(optionalAttrs (go && ISA == "i686") {
|
||||
GO386 = "sse2";
|
||||
})
|
||||
(optionalAttrs go {
|
||||
GCCGO = "gccgo";
|
||||
CGO_CFLAGS_ALLOW = "-f.*";
|
||||
CGO_CXXFLAGS_ALLOW = "-f.*";
|
||||
CGO_CPPFLAGS_ALLOW = "-D.*";
|
||||
CGO_LDFLAGS_ALLOW = "-Wl.*";
|
||||
})
|
||||
(addMarchSpecific march)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,14 @@
|
|||
with lib;
|
||||
{
|
||||
package' = types.package // {
|
||||
merge = loc: defs:
|
||||
let res = mergeDefaultOption loc defs;
|
||||
in if builtins.isPath res || (builtins.isString res && ! builtins.hasContext res)
|
||||
then toDerivation res
|
||||
else res;
|
||||
merge =
|
||||
loc: defs:
|
||||
let
|
||||
res = mergeDefaultOption loc defs;
|
||||
in
|
||||
if builtins.isPath res || (builtins.isString res && !builtins.hasContext res) then
|
||||
toDerivation res
|
||||
else
|
||||
res;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue