##// END OF EJS Templates
licenses: Update license.nix to work with new enterprise derivation.
johbo -
r198:f1210dd3 default
parent child Browse files
Show More
@@ -1,163 +1,160 b''
1 1 # Utility to generate the license information
2 2 #
3 3 # Usage:
4 4 #
5 5 # nix-build -I ~/dev license.nix -A result
6 6 #
7 7 # Afterwards ./result will contain the license information as JSON files.
8 8 #
9 9 #
10 10 # Overview
11 11 #
12 12 # Uses two steps to get the relevant license information:
13 13 #
14 14 # 1. Walk down the derivations based on "buildInputs" and
15 15 # "propagatedBuildInputs". This results in all dependencies based on the nix
16 16 # declartions.
17 17 #
18 18 # 2. Build Enterprise and query nix-store to get a list of runtime
19 19 # dependencies. The results from step 1 are then limited to the ones which
20 20 # are in this list.
21 21 #
22 22 # The result is then available in ./result/license.json.
23 23 #
24 24
25 25
26 26 let
27 27
28 28 nixpkgs = import <nixpkgs> {};
29 29
30 30 stdenv = nixpkgs.stdenv;
31 31
32 32 # Enterprise as simple as possible, goal here is just to identify the runtime
33 33 # dependencies. Ideally we could avoid building Enterprise at all and somehow
34 34 # figure it out without calling into nix-store.
35 35 enterprise = import ./default.nix {
36 36 doCheck = false;
37 with_vcsserver = false;
38 with_pyramid = false;
39 cythonize = false;
40 37 };
41 38
42 39 # For a given derivation, return the list of all dependencies
43 40 drvToDependencies = drv: nixpkgs.lib.flatten [
44 41 drv.nativeBuildInputs or []
45 42 drv.propagatedNativeBuildInputs or []
46 43 ];
47 44
48 45 # Transform the given derivation into the meta information which we need in
49 46 # the resulting JSON files.
50 47 drvToMeta = drv: {
51 48 name = drv.name or "UNNAMED";
52 49 license = if drv ? meta.license then drv.meta.license else "UNKNOWN";
53 50 };
54 51
55 52 # Walk the tree of buildInputs and propagatedBuildInputs and return it as a
56 53 # flat list. Duplicates are avoided.
57 54 listDrvDependencies = drv: let
58 55 addElement = element: seen:
59 56 if (builtins.elem element seen)
60 57 then seen
61 58 else let
62 59 newSeen = seen ++ [ element ];
63 60 newDeps = drvToDependencies element;
64 61 in nixpkgs.lib.fold addElement newSeen newDeps;
65 62 initialElements = drvToDependencies drv;
66 63 in nixpkgs.lib.fold addElement [] initialElements;
67 64
68 65 # Reads in a file with store paths and returns a list of derivation names.
69 66 #
70 67 # Reads the file, splits the lines, then removes the prefix, so that we
71 68 # end up with a list of derivation names in the end.
72 69 storePathsToDrvNames = srcPath: let
73 70 rawStorePaths = nixpkgs.lib.removeSuffix "\n" (
74 71 builtins.readFile srcPath);
75 72 storePaths = nixpkgs.lib.splitString "\n" rawStorePaths;
76 73 # TODO: johbo: Would be nice to use some sort of utility here to convert
77 74 # the path to a derivation name.
78 75 storePathPrefix = (
79 76 builtins.stringLength "/nix/store/zwy7aavnif9ayw30rya1k6xiacafzzl6-");
80 77 storePathToName = path:
81 78 builtins.substring storePathPrefix (builtins.stringLength path) path;
82 79 in (map storePathToName storePaths);
83 80
84 81 in rec {
85 82
86 83 # Build Enterprise and call nix-store to retrieve the runtime
87 84 # dependencies. The result is available in the nix store.
88 85 runtimeDependencies = stdenv.mkDerivation {
89 86 name = "runtime-dependencies";
90 87 buildInputs = [
91 88 # Needed to query the store
92 89 nixpkgs.nix
93 90 ];
94 91 unpackPhase = ''
95 92 echo "Nothing to unpack"
96 93 '';
97 94 buildPhase = ''
98 95 # Get a list of runtime dependencies
99 96 nix-store -q --references ${enterprise} > nix-store-references
100 97 '';
101 98 installPhase = ''
102 99 mkdir -p $out
103 100 cp -v nix-store-references $out/
104 101 '';
105 102 };
106 103
107 104 # Produce the license overview files.
108 105 result = let
109 106
110 107 # Dependencies according to the nix-store
111 108 runtimeDependencyNames = (
112 109 storePathsToDrvNames "${runtimeDependencies}/nix-store-references");
113 110
114 111 # Dependencies based on buildInputs and propagatedBuildInputs
115 112 enterpriseAllDependencies = listDrvDependencies enterprise;
116 113 enterpriseRuntimeDependencies = let
117 114 elemName = element: element.name or "UNNAMED";
118 115 isRuntime = element: builtins.elem (elemName element) runtimeDependencyNames;
119 116 in builtins.filter isRuntime enterpriseAllDependencies;
120 117
121 118 # Extract relevant meta information
122 119 enterpriseAllLicenses = map drvToMeta enterpriseAllDependencies;
123 120 enterpriseRuntimeLicenses = map drvToMeta enterpriseRuntimeDependencies;
124 121
125 122 in stdenv.mkDerivation {
126 123
127 124 name = "licenses";
128 125
129 126 buildInputs = [];
130 127
131 128 unpackPhase = ''
132 129 echo "Nothing to unpack"
133 130 '';
134 131
135 132 buildPhase = ''
136 133 mkdir build
137 134
138 135 # Copy list of runtime dependencies for the Python processor
139 136 cp "${runtimeDependencies}/nix-store-references" ./build/nix-store-references
140 137
141 138 # All licenses which we found by walking buildInputs and
142 139 # propagatedBuildInputs
143 140 cat > build/all-licenses.json <<EOF
144 141 ${builtins.toJSON enterpriseAllLicenses}
145 142 EOF
146 143
147 144 # License information for our runtime dependencies only. Basically all
148 145 # licenses limited to the items which where also reported by nix-store as
149 146 # a dependency.
150 147 cat > build/licenses.json <<EOF
151 148 ${builtins.toJSON enterpriseRuntimeLicenses}
152 149 EOF
153 150 '';
154 151
155 152 installPhase = ''
156 153 mkdir -p $out
157 154
158 155 # Store it all, that helps when things go wrong
159 156 cp -rv ./build/* $out
160 157 '';
161 158 };
162 159
163 160 }
General Comments 0
You need to be logged in to leave comments. Login now