about summary refs log tree commit diff
path: root/tools/nixery/build-image/load-pkgs.nix
blob: 3e8b450c45d281c262284d4ae140e5aa68416db5 (plain) (blame)
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
# 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.

# Load a Nix package set from a source specified in one of the following
# formats:
#
# 1. nixpkgs!$channel (e.g. nixpkgs!nixos-19.03)
# 2. git!$repo!$rev (e.g. git!git@github.com:NixOS/nixpkgs.git!master)
# 3. path!$path (e.g. path!/var/local/nixpkgs)
#
# '!' was chosen as the separator because `builtins.split` does not
# support regex escapes and there are few other candidates. It
# doesn't matter much because this is invoked by the server.
{ pkgSource, args ? { } }:

with builtins;
let
  # If a nixpkgs channel is requested, it is retrieved from Github (as
  # a tarball) and imported.
  fetchImportChannel = channel:
    let
      url =
        "https://github.com/NixOS/nixpkgs-channels/archive/${channel}.tar.gz";
    in import (fetchTarball url) args;

  # If a git repository is requested, it is retrieved via
  # builtins.fetchGit which defaults to the git configuration of the
  # outside environment. This means that user-configured SSH
  # credentials etc. are going to work as expected.
  fetchImportGit = url: rev:
    let
      # builtins.fetchGit needs to know whether 'rev' is a reference
      # (e.g. a branch/tag) or a revision (i.e. a commit hash)
      #
      # Since this data is being extrapolated from the supplied image
      # tag, we have to guess if we want to avoid specifying a format.
      #
      # There are some additional caveats around whether the default
      # branch contains the specified revision, which need to be
      # explained to users.
      spec = if (stringLength rev) == 40 then {
        inherit url rev;
      } else {
        inherit url;
        ref = rev;
      };
    in import (fetchGit spec) args;

  # 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) args;

  source = split "!" pkgSource;
  sourceType = elemAt source 0;
in if sourceType == "nixpkgs" then
  fetchImportChannel (elemAt source 2)
else if sourceType == "git" then
  fetchImportGit (elemAt source 2) (elemAt source 4)
else if sourceType == "path" then
  importPath (elemAt source 2)
else
  throw ("Invalid package set source specification: ${pkgSource}")