-
Notifications
You must be signed in to change notification settings - Fork 7
/
lib.nix
119 lines (108 loc) · 4.3 KB
/
lib.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
let
# Allow overriding pinned nixpkgs for debugging purposes via cardano_pkgs
fetchNixPkgs = let try = builtins.tryEval <cardano_pkgs>;
in if try.success
then builtins.trace "using host <cardano_pkgs>" try.value
else import ./nix/fetch-nixpkgs.nix;
# Function to import the pinned nixpkgs with necessary overlays,
# applied to the given args.
importPkgs = args: import fetchNixPkgs ({ overlays = [ (import ./nix/overlays/jemalloc.nix) ]; } // args);
# Gets the value of an environment variable, with a default if it's
# unset or empty.
maybeEnv = env: default:
let
result = builtins.getEnv env;
in if result != ""
then result
else default;
# Removes files within a Haskell source tree which won't change the
# result of building the package.
# This is so that cached build products can be used whenever possible.
# It also applies the lib.cleanSource filter from nixpkgs which
# removes VCS directories, emacs backup files, etc.
cleanSourceTree = src:
if (builtins.typeOf src) == "path"
then lib.cleanSourceWith {
filter = with pkgs.stdenv;
name: type: let baseName = baseNameOf (toString name); in ! (
# Filter out cabal build products.
baseName == "dist" || baseName == "dist-newstyle" ||
baseName == "cabal.project.local" ||
lib.hasPrefix ".ghc.environment" baseName ||
# Filter out stack build products.
lib.hasPrefix ".stack-work" baseName ||
# Filter out files which are commonly edited but don't
# affect the cabal build.
lib.hasSuffix ".nix" baseName
);
src = lib.cleanSource src;
} else src;
pkgs = import fetchNixPkgs {};
lib = pkgs.lib;
in lib // (rec {
inherit fetchNixPkgs importPkgs cleanSourceTree;
# cardano-crypto is not a cardano-sl package.
isCardanoSL = name: lib.hasPrefix "cardano-" name && !(name == "cardano-crypto");
isBenchmark = args: !((args.isExecutable or false) || (args.isLibrary or true));
# Insert this into builder scripts where programs require a UTF-8
# locale to work.
utf8LocaleSetting = ''
export LC_ALL=en_GB.UTF-8
export LANG=en_GB.UTF-8
'';
# Blockchain networks and their configuration keys
environments = {
mainnet = {
relays = "relays.cardano-mainnet.iohk.io";
confKey = "mainnet_full";
private = false;
};
staging = {
relays = "relays.awstest.iohkdev.io";
confKey = "mainnet_dryrun_full";
private = false;
};
testnet = {
relays = "relays.cardano-testnet.iohkdev.io";
confKey = "testnet_full";
private = false;
};
demo = {
confKey = "dev";
relays = "127.0.0.1";
private = true;
};
};
# Generates an attrset for the three cardano-sl networks by applying
# the given function to each environment.
#
# Example:
# forEnvironments ({ environment, confKey, ... }: "key for ${environment} is ${confKey}")
# => { demo = "key for demo is dev";
# mainnet = "key for mainnet is mainnet_full";
# staging = "key for staging is mainnet_dryrun_full";
# testnet = "key for testnet is testnet_full"; }
forEnvironments = f: lib.mapAttrs
(name: env: f (env // { environment = name; }))
environments;
runHaskell = name: hspkgs: deps: env: code: let
ghc = hspkgs.ghcWithPackages deps;
flags = lib.optionalString pkgs.stdenv.isDarwin "-liconv"; # https://github.com/NixOS/nixpkgs/issues/46814
builtBinary = pkgs.runCommand "${name}-binary" { buildInputs = [ ghc ]; } ''
mkdir -p $out/bin/
ghc ${pkgs.writeText "${name}.hs" code} ${flags} -o $out/bin/${name}
'';
in pkgs.runCommand name env ''
${builtBinary}/bin/$name
'';
# Overrides the lib.commitIdFromGitRepo function in nixpkgs
commitIdFromGitRepo = import ./nix/commit-id.nix { inherit lib; };
# Plucks all the cardano-sl packages from the haskellPackages set,
# also adding -static variants which contain the gitrev.
getCardanoPackages = justStaticExecutablesGitRev: haskellPackages:
let
cslPackages = lib.filterAttrs (name: drv: isCardanoSL name) haskellPackages;
makeStatic = name: drv: lib.nameValuePair (name + "-static") (justStaticExecutablesGitRev drv);
in
cslPackages // lib.mapAttrs' makeStatic cslPackages;
})