4 nix-shell maintainers/scripts/update.nix
6 See https://nixos.org/manual/nixpkgs/unstable/#var-passthru-updateScript
13 , include-overlays ? false
20 pkgs = import ./../../default.nix (
21 if include-overlays == false then
23 else if include-overlays == true then
24 { } # Let Nixpkgs include overlays impurely.
25 else { overlays = include-overlays; }
30 /* Remove duplicate elements from the list based on some extracted value. O(n^2) complexity.
38 xs = lib.filter (p: f x != f p) (lib.drop 1 list);
42 /* Recursively find all packages (derivations) in `pkgs` matching `cond` predicate.
44 Type: packagesWithPath :: AttrPath → (AttrPath → derivation → bool) → AttrSet → List<AttrSet{attrPath :: str; package :: derivation; }>
47 The packages will be returned as a list of named pairs comprising of:
48 - attrPath: stringified attribute path (based on `rootPath`)
49 - package: corresponding derivation
51 packagesWithPath = rootPath: cond: pkgs:
53 packagesWithPathInner = path: pathContent:
55 result = builtins.tryEval pathContent;
57 somewhatUniqueRepresentant =
58 { package, attrPath }: {
59 inherit (package) updateScript;
60 # Some updaters use the same `updateScript` value for all packages.
61 # Also compare `meta.description`.
62 position = package.meta.position or null;
63 # We cannot always use `meta.position` since it might not be available
64 # or it might be shared among multiple packages.
67 dedupResults = lst: nubOn somewhatUniqueRepresentant (lib.concatLists lst);
69 if result.success then
71 evaluatedPathContent = result.value;
73 if lib.isDerivation evaluatedPathContent then
74 lib.optional (cond path evaluatedPathContent) { attrPath = lib.concatStringsSep "." path; package = evaluatedPathContent; }
75 else if lib.isAttrs evaluatedPathContent then
76 # If user explicitly points to an attrSet or it is marked for recursion, we recur.
77 if path == rootPath || evaluatedPathContent.recurseForDerivations or false || evaluatedPathContent.recurseForRelease or false then
78 dedupResults (lib.mapAttrsToList (name: elem: packagesWithPathInner (path ++ [name]) elem) evaluatedPathContent)
83 packagesWithPathInner rootPath pkgs;
85 /* Recursively find all packages (derivations) in `pkgs` matching `cond` predicate.
87 packagesWith = packagesWithPath [];
89 /* Recursively find all packages in `pkgs` with updateScript matching given predicate.
91 packagesWithUpdateScriptMatchingPredicate = cond:
92 packagesWith (path: pkg: builtins.hasAttr "updateScript" pkg && cond path pkg);
94 /* Recursively find all packages in `pkgs` with updateScript by given maintainer.
96 packagesWithUpdateScriptAndMaintainer = maintainer':
99 if ! builtins.hasAttr maintainer' lib.maintainers then
100 builtins.throw "Maintainer with name `${maintainer'} does not exist in `maintainers/maintainer-list.nix`."
102 builtins.getAttr maintainer' lib.maintainers;
104 packagesWithUpdateScriptMatchingPredicate (path: pkg:
105 (if builtins.hasAttr "maintainers" pkg.meta
106 then (if builtins.isList pkg.meta.maintainers
107 then builtins.elem maintainer pkg.meta.maintainers
108 else maintainer == pkg.meta.maintainers
114 /* Recursively find all packages under `path` in `pkgs` with updateScript.
116 packagesWithUpdateScript = path: pkgs:
118 prefix = lib.splitString "." path;
119 pathContent = lib.attrByPath prefix null pkgs;
121 if pathContent == null then
122 builtins.throw "Attribute path `${path}` does not exist."
124 packagesWithPath prefix (path: pkg: builtins.hasAttr "updateScript" pkg)
127 /* Find a package under `path` in `pkgs` and require that it has an updateScript.
129 packageByName = path: pkgs:
131 package = lib.attrByPath (lib.splitString "." path) null pkgs;
133 if package == null then
134 builtins.throw "Package with an attribute name `${path}` does not exist."
135 else if ! builtins.hasAttr "updateScript" package then
136 builtins.throw "Package with an attribute name `${path}` does not have a `passthru.updateScript` attribute defined."
138 { attrPath = path; inherit package; };
140 /* List of packages matched based on the CLI arguments.
143 if package != null then
144 [ (packageByName package pkgs) ]
145 else if predicate != null then
146 packagesWithUpdateScriptMatchingPredicate predicate pkgs
147 else if maintainer != null then
148 packagesWithUpdateScriptAndMaintainer maintainer pkgs
149 else if path != null then
150 packagesWithUpdateScript path pkgs
152 builtins.throw "No arguments provided.\n\n${helpText}";
157 % nix-shell maintainers/scripts/update.nix --argstr maintainer garbas
159 to run all update scripts for all packages that lists \`garbas\` as a maintainer
160 and have \`updateScript\` defined, or:
162 % nix-shell maintainers/scripts/update.nix --argstr package nautilus
164 to run update script for specific package, or
166 % nix-shell maintainers/scripts/update.nix --arg predicate '(path: pkg: pkg.updateScript.name or null == "gnome-update-script")'
168 to run update script for all packages matching given predicate, or
170 % nix-shell maintainers/scripts/update.nix --argstr path gnome
172 to run update script for all package under an attribute path.
176 --argstr max-workers 8
178 to increase the number of jobs in parallel, or
180 --argstr keep-going true
182 to continue running when a single update fails.
184 You can also make the updater automatically commit on your behalf from updateScripts
185 that support it by adding
191 --argstr skip-prompt true
194 /* Transform a matched package into an object for update.py.
196 packageData = { package, attrPath }: {
198 pname = lib.getName package;
199 oldVersion = lib.getVersion package;
200 updateScript = map builtins.toString (lib.toList (package.updateScript.command or package.updateScript));
201 supportedFeatures = package.updateScript.supportedFeatures or [];
202 attrPath = package.updateScript.attrPath or attrPath;
205 /* JSON file with data for update.py.
207 packagesJson = pkgs.writeText "packages.json" (builtins.toJSON (map packageData packages));
210 lib.optional (max-workers != null) "--max-workers=${max-workers}"
211 ++ lib.optional (keep-going == "true") "--keep-going"
212 ++ lib.optional (commit == "true") "--commit"
213 ++ lib.optional (skip-prompt == "true") "--skip-prompt";
215 args = [ packagesJson ] ++ optionalArgs;
217 in pkgs.stdenv.mkDerivation {
218 name = "nixpkgs-update-script";
221 echo "----------------------------------------------------------------"
223 echo "Not possible to update packages using \`nix-build\`"
226 echo "----------------------------------------------------------------"
230 unset shellHook # do not contaminate nested shells
231 exec ${pkgs.python3.interpreter} ${./update.py} ${builtins.concatStringsSep " " args}