4 nix-shell maintainers/scripts/update.nix
6 See https://nixos.org/manual/nixpkgs/unstable/#var-passthru-updateScript
11 , get-script ? pkg: pkg.updateScript or null
14 , include-overlays ? false
21 pkgs = import ./../../default.nix ((
22 if include-overlays == false then
24 else if include-overlays == true then
25 { } # Let Nixpkgs include overlays impurely.
26 else { overlays = include-overlays; }
27 ) // { config.allowAliases = false; });
31 /* Remove duplicate elements from the list based on some extracted value. O(n^2) complexity.
39 xs = lib.filter (p: f x != f p) (lib.drop 1 list);
43 /* Recursively find all packages (derivations) in `pkgs` matching `cond` predicate.
45 Type: packagesWithPath :: AttrPath → (AttrPath → derivation → bool) → AttrSet → List<AttrSet{attrPath :: str; package :: derivation; }>
48 The packages will be returned as a list of named pairs comprising of:
49 - attrPath: stringified attribute path (based on `rootPath`)
50 - package: corresponding derivation
52 packagesWithPath = rootPath: cond: pkgs:
54 packagesWithPathInner = path: pathContent:
56 result = builtins.tryEval pathContent;
58 somewhatUniqueRepresentant =
59 { package, attrPath }: {
60 updateScript = (get-script package);
61 # Some updaters use the same `updateScript` value for all packages.
62 # Also compare `meta.description`.
63 position = package.meta.position or null;
64 # We cannot always use `meta.position` since it might not be available
65 # or it might be shared among multiple packages.
68 dedupResults = lst: nubOn somewhatUniqueRepresentant (lib.concatLists lst);
70 if result.success then
72 evaluatedPathContent = result.value;
74 if lib.isDerivation evaluatedPathContent then
75 lib.optional (cond path evaluatedPathContent) { attrPath = lib.concatStringsSep "." path; package = evaluatedPathContent; }
76 else if lib.isAttrs evaluatedPathContent then
77 # If user explicitly points to an attrSet or it is marked for recursion, we recur.
78 if path == rootPath || evaluatedPathContent.recurseForDerivations or false || evaluatedPathContent.recurseForRelease or false then
79 dedupResults (lib.mapAttrsToList (name: elem: packagesWithPathInner (path ++ [name]) elem) evaluatedPathContent)
84 packagesWithPathInner rootPath pkgs;
86 /* Recursively find all packages (derivations) in `pkgs` matching `cond` predicate.
88 packagesWith = packagesWithPath [];
90 /* Recursively find all packages in `pkgs` with updateScript matching given predicate.
92 packagesWithUpdateScriptMatchingPredicate = cond:
93 packagesWith (path: pkg: (get-script pkg != null) && cond path pkg);
95 /* Recursively find all packages in `pkgs` with updateScript by given maintainer.
97 packagesWithUpdateScriptAndMaintainer = maintainer':
100 if ! builtins.hasAttr maintainer' lib.maintainers then
101 builtins.throw "Maintainer with name `${maintainer'} does not exist in `maintainers/maintainer-list.nix`."
103 builtins.getAttr maintainer' lib.maintainers;
105 packagesWithUpdateScriptMatchingPredicate (path: pkg:
106 (if builtins.hasAttr "maintainers" pkg.meta
107 then (if builtins.isList pkg.meta.maintainers
108 then builtins.elem maintainer pkg.meta.maintainers
109 else maintainer == pkg.meta.maintainers
115 /* Recursively find all packages under `path` in `pkgs` with updateScript.
117 packagesWithUpdateScript = path: pkgs:
119 prefix = lib.splitString "." path;
120 pathContent = lib.attrByPath prefix null pkgs;
122 if pathContent == null then
123 builtins.throw "Attribute path `${path}` does not exist."
125 packagesWithPath prefix (path: pkg: (get-script pkg != null))
128 /* Find a package under `path` in `pkgs` and require that it has an updateScript.
130 packageByName = path: pkgs:
132 package = lib.attrByPath (lib.splitString "." path) null pkgs;
134 if package == null then
135 builtins.throw "Package with an attribute name `${path}` does not exist."
136 else if get-script package == null then
137 builtins.throw "Package with an attribute name `${path}` does not have a `passthru.updateScript` attribute defined."
139 { attrPath = path; inherit package; };
141 /* List of packages matched based on the CLI arguments.
144 if package != null then
145 [ (packageByName package pkgs) ]
146 else if predicate != null then
147 packagesWithUpdateScriptMatchingPredicate predicate pkgs
148 else if maintainer != null then
149 packagesWithUpdateScriptAndMaintainer maintainer pkgs
150 else if path != null then
151 packagesWithUpdateScript path pkgs
153 builtins.throw "No arguments provided.\n\n${helpText}";
158 % nix-shell maintainers/scripts/update.nix --argstr maintainer garbas
160 to run all update scripts for all packages that lists \`garbas\` as a maintainer
161 and have \`updateScript\` defined, or:
163 % nix-shell maintainers/scripts/update.nix --argstr package nautilus
165 to run update script for specific package, or
167 % nix-shell maintainers/scripts/update.nix --arg predicate '(path: pkg: pkg.updateScript.name or null == "gnome-update-script")'
169 to run update script for all packages matching given predicate, or
171 % nix-shell maintainers/scripts/update.nix --argstr path gnome
173 to run update script for all package under an attribute path.
177 --argstr max-workers 8
179 to increase the number of jobs in parallel, or
181 --argstr keep-going true
183 to continue running when a single update fails.
185 You can also make the updater automatically commit on your behalf from updateScripts
186 that support it by adding
192 --argstr skip-prompt true
195 /* Transform a matched package into an object for update.py.
197 packageData = { package, attrPath }: let updateScript = get-script package; in {
199 pname = lib.getName package;
200 oldVersion = lib.getVersion package;
201 updateScript = map builtins.toString (lib.toList (updateScript.command or updateScript));
202 supportedFeatures = updateScript.supportedFeatures or [];
203 attrPath = updateScript.attrPath or attrPath;
206 /* JSON file with data for update.py.
208 packagesJson = pkgs.writeText "packages.json" (builtins.toJSON (map packageData packages));
211 lib.optional (max-workers != null) "--max-workers=${max-workers}"
212 ++ lib.optional (keep-going == "true") "--keep-going"
213 ++ lib.optional (commit == "true") "--commit"
214 ++ lib.optional (skip-prompt == "true") "--skip-prompt";
216 args = [ packagesJson ] ++ optionalArgs;
218 in pkgs.stdenv.mkDerivation {
219 name = "nixpkgs-update-script";
222 echo "----------------------------------------------------------------"
224 echo "Not possible to update packages using \`nix-build\`"
227 echo "----------------------------------------------------------------"
231 unset shellHook # do not contaminate nested shells
232 exec ${pkgs.python3.interpreter} ${./update.py} ${builtins.concatStringsSep " " args}
234 nativeBuildInputs = [ pkgs.git pkgs.nix pkgs.cacert ];