about summary refs log tree commit diff
path: root/tools/nixery/prepare-image
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-04-20T14·41+0200
committerclbot <clbot@tvl.fyi>2022-04-20T15·31+0000
commit6716bf018c465f4b943eef0e6716caa7a54940da (patch)
tree8e609da0ed0052f4088250ecc1033c28d35f5d6c /tools/nixery/prepare-image
parent535ad8732a688bf6ff4c48283d89dae00262a719 (diff)
chore(nixery): Housekeeping for depot compatibility r/3982
Cleans up a whole bunch of things I wanted to get out of the door
right away:

* depot internal references to //third_party/nixery have been replaced
  with //tools/nixery
* cleaned up files from Github
* fixed SPDX & Copyright headers
* code formatting and inclusion in //tools/depotfmt checks

Change-Id: Iea79f0fdf3aa04f71741d4f4032f88605ae415bb
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5486
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Autosubmit: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tools/nixery/prepare-image')
-rw-r--r--tools/nixery/prepare-image/default.nix17
-rw-r--r--tools/nixery/prepare-image/load-pkgs.nix21
-rw-r--r--tools/nixery/prepare-image/prepare-image.nix116
3 files changed, 66 insertions, 88 deletions
diff --git a/tools/nixery/prepare-image/default.nix b/tools/nixery/prepare-image/default.nix
index 60b208f522..efd9ed3404 100644
--- a/tools/nixery/prepare-image/default.nix
+++ b/tools/nixery/prepare-image/default.nix
@@ -1,16 +1,5 @@
-# Copyright 2019 Google LLC
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     https://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
+# Copyright 2022 The TVL Contributors
+# SPDX-License-Identifier: Apache-2.0
 
 # This file builds a wrapper script called by Nixery to ask for the
 # content information for a given image.
@@ -18,7 +7,7 @@
 # The purpose of using a wrapper script is to ensure that the paths to
 # all required Nix files are set correctly at runtime.
 
-{ pkgs ? import <nixpkgs> {} }:
+{ pkgs ? import <nixpkgs> { } }:
 
 pkgs.writeShellScriptBin "nixery-prepare-image" ''
   exec ${pkgs.nix}/bin/nix-build \
diff --git a/tools/nixery/prepare-image/load-pkgs.nix b/tools/nixery/prepare-image/load-pkgs.nix
index 4a89dcde3a..7f8ab5479d 100644
--- a/tools/nixery/prepare-image/load-pkgs.nix
+++ b/tools/nixery/prepare-image/load-pkgs.nix
@@ -1,16 +1,5 @@
-# Copyright 2019 Google LLC
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     https://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
+# Copyright 2022 The TVL Contributors
+# SPDX-License-Identifier: Apache-2.0
 
 # Load a Nix package set from one of the supported source types
 # (nixpkgs, git, path).
@@ -24,7 +13,8 @@ let
     let
       url =
         "https://github.com/NixOS/nixpkgs/archive/${channel}.tar.gz";
-    in import (fetchTarball url) importArgs;
+    in
+    import (fetchTarball url) importArgs;
 
   # If a git repository is requested, it is retrieved via
   # builtins.fetchGit which defaults to the git configuration of the
@@ -35,7 +25,8 @@ let
   # No special handling is used for paths, so users are expected to pass one
   # that will work natively with Nix.
   importPath = path: import (toPath path) importArgs;
-in if srcType == "nixpkgs" then
+in
+if srcType == "nixpkgs" then
   fetchImportChannel srcArgs
 else if srcType == "git" then
   fetchImportGit (fromJSON srcArgs)
diff --git a/tools/nixery/prepare-image/prepare-image.nix b/tools/nixery/prepare-image/prepare-image.nix
index acd1430548..bb88983cf6 100644
--- a/tools/nixery/prepare-image/prepare-image.nix
+++ b/tools/nixery/prepare-image/prepare-image.nix
@@ -1,16 +1,5 @@
-# Copyright 2019 Google LLC
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     https://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
+# Copyright 2022 The TVL Contributors
+# SPDX-License-Identifier: Apache-2.0
 
 # This file contains a derivation that outputs structured information
 # about the runtime dependencies of an image with a given set of
@@ -23,13 +12,13 @@
 
 {
   # Description of the package set to be used (will be loaded by load-pkgs.nix)
-  srcType ? "nixpkgs",
-  srcArgs ? "nixos-20.09",
-  system ? "x86_64-linux",
-  importArgs ? { },
-  # Path to load-pkgs.nix
-  loadPkgs ? ./load-pkgs.nix,
-  # Packages to install by name (which must refer to top-level attributes of
+  srcType ? "nixpkgs"
+, srcArgs ? "nixos-20.09"
+, system ? "x86_64-linux"
+, importArgs ? { }
+, # Path to load-pkgs.nix
+  loadPkgs ? ./load-pkgs.nix
+, # Packages to install by name (which must refer to top-level attributes of
   # nixpkgs). This is passed in as a JSON-array in string form.
   packages ? "[]"
 }:
@@ -77,24 +66,28 @@ let
   # `deepFetch haskellpackages.stylish-haskell` retrieves
   # `haskellPackages.stylish-haskell`.
   deepFetch = with lib; s: n:
-    let path = splitString "." n;
-        err = { error = "not_found"; pkg = n; };
-        # The most efficient way I've found to do a lookup against
-        # case-differing versions of an attribute is to first construct a
-        # mapping of all lowercased attribute names to their differently cased
-        # equivalents.
-        #
-        # This map is then used for a second lookup if the top-level
-        # (case-sensitive) one does not yield a result.
-        hasUpper = str: (match ".*[A-Z].*" str) != null;
-        allUpperKeys = filter hasUpper (attrNames s);
-        lowercased = listToAttrs (map (k: {
+    let
+      path = splitString "." n;
+      err = { error = "not_found"; pkg = n; };
+      # The most efficient way I've found to do a lookup against
+      # case-differing versions of an attribute is to first construct a
+      # mapping of all lowercased attribute names to their differently cased
+      # equivalents.
+      #
+      # This map is then used for a second lookup if the top-level
+      # (case-sensitive) one does not yield a result.
+      hasUpper = str: (match ".*[A-Z].*" str) != null;
+      allUpperKeys = filter hasUpper (attrNames s);
+      lowercased = listToAttrs (map
+        (k: {
           name = toLower k;
           value = k;
-          }) allUpperKeys);
-        caseAmendedPath = map (v: if hasAttr v lowercased then lowercased."${v}" else v) path;
-        fetchLower = attrByPath caseAmendedPath err s;
-    in attrByPath path fetchLower s;
+        })
+        allUpperKeys);
+      caseAmendedPath = map (v: if hasAttr v lowercased then lowercased."${v}" else v) path;
+      fetchLower = attrByPath caseAmendedPath err s;
+    in
+    attrByPath path fetchLower s;
 
   # allContents contains all packages successfully retrieved by name
   # from the package set, as well as any errors encountered while
@@ -105,27 +98,30 @@ let
     # Folds over the results of 'deepFetch' on all requested packages to
     # separate them into errors and content. This allows the program to
     # terminate early and return only the errors if any are encountered.
-    let splitter = attrs: res:
-          if hasAttr "error" res
-          then attrs // { errors = attrs.errors ++ [ res ]; }
-          else attrs // { contents = attrs.contents ++ [ res ]; };
-        init = { contents = []; errors = []; };
-        fetched = (map (deepFetch pkgs) (fromJSON packages));
-    in foldl' splitter init fetched;
+    let
+      splitter = attrs: res:
+        if hasAttr "error" res
+        then attrs // { errors = attrs.errors ++ [ res ]; }
+        else attrs // { contents = attrs.contents ++ [ res ]; };
+      init = { contents = [ ]; errors = [ ]; };
+      fetched = (map (deepFetch pkgs) (fromJSON packages));
+    in
+    foldl' splitter init fetched;
 
   # Contains the export references graph of all retrieved packages,
   # which has information about all runtime dependencies of the image.
   #
   # This is used by Nixery to group closures into image layers.
-  runtimeGraph = runCommand "runtime-graph.json" {
-    __structuredAttrs = true;
-    exportReferencesGraph.graph = allContents.contents;
-    PATH = "${coreutils}/bin";
-    builder = toFile "builder" ''
-      . .attrs.sh
-      cp .attrs.json ''${outputs[out]}
-    '';
-  } "";
+  runtimeGraph = runCommand "runtime-graph.json"
+    {
+      __structuredAttrs = true;
+      exportReferencesGraph.graph = allContents.contents;
+      PATH = "${coreutils}/bin";
+      builder = toFile "builder" ''
+        . .attrs.sh
+        cp .attrs.json ''${outputs[out]}
+      '';
+    } "";
 
   # Create a symlink forest into all top-level store paths of the
   # image contents.
@@ -151,7 +147,7 @@ let
   # Image layer that contains the symlink forest created above. This
   # must be included in the image to ensure that the filesystem has a
   # useful layout at runtime.
-  symlinkLayer = runCommand "symlink-layer.tar" {} ''
+  symlinkLayer = runCommand "symlink-layer.tar" { } ''
     cp -r ${contentsEnv}/ ./layer
     tar --transform='s|^\./||' -C layer --sort=name --mtime="@$SOURCE_DATE_EPOCH" --owner=0 --group=0 -cf $out .
   '';
@@ -159,9 +155,10 @@ let
   # Metadata about the symlink layer which is required for serving it.
   # Two different hashes are computed for different usages (inclusion
   # in manifest vs. content-checking in the layer cache).
-  symlinkLayerMeta = fromJSON (readFile (runCommand "symlink-layer-meta.json" {
-    buildInputs = [ coreutils jq openssl ];
-  }''
+  symlinkLayerMeta = fromJSON (readFile (runCommand "symlink-layer-meta.json"
+    {
+      buildInputs = [ coreutils jq openssl ];
+    } ''
     tarHash=$(sha256sum ${symlinkLayer} | cut -d ' ' -f1)
     layerSize=$(stat --printf '%s' ${symlinkLayer})
 
@@ -181,7 +178,8 @@ let
     error = "not_found";
     pkgs = map (err: err.pkg) allContents.errors;
   };
-in writeText "build-output.json" (if (length allContents.errors) == 0
-  then toJSON buildOutput
-  else toJSON errorOutput
+in
+writeText "build-output.json" (if (length allContents.errors) == 0
+then toJSON buildOutput
+else toJSON errorOutput
 )