From 99d8fc8a85910f7945a463ff6114404a085b7073 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Thu, 11 Jan 2024 23:21:49 -0800 Subject: [PATCH] Use mocked builders to get default pkg name faster Since evaluating pkgs is expensive, this speeds up evaluation of flakes with `packages.default` by attempting to get the name using a pkgs set containing only mocked builders that just return the name. --- builtinModules/packages.nix | 6 +++-- misc/nameMockedPkgs.nix | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 misc/nameMockedPkgs.nix diff --git a/builtinModules/packages.nix b/builtinModules/packages.nix index cdbaf13..1a38df1 100644 --- a/builtinModules/packages.nix +++ b/builtinModules/packages.nix @@ -5,8 +5,8 @@ { config, lib, inputs, flakelight, genSystems, ... }: let inherit (builtins) parseDrvName tryEval; - inherit (lib) filterAttrs findFirst mapAttrs mapAttrs' mkIf mkMerge - mkOption nameValuePair optionalAttrs; + inherit (lib) filterAttrs findFirst mapAttrs mapAttrs' mkIf mkMerge mkOption + nameValuePair optionalAttrs; inherit (lib.types) lazyAttrsOf nullOr str uniq; inherit (flakelight) supportedSystem; inherit (flakelight.types) overlay packageDef; @@ -49,12 +49,14 @@ in getName = pkg: pkg.pname or (parseDrvName pkg.name).name; inherit (prev.stdenv.hostPlatform) system; baseNixpkgs = inputs.nixpkgs.legacyPackages.${system}; + mockPkgs = import ../misc/nameMockedPkgs.nix prev; defaultPkgName = findFirst (x: (tryEval x).success) (throw ("Could not determine the name of the default package; " + "please set the `pname` flakelight option to the intended name.")) [ (assert config.pname != null; config.pname) + (getName (mockPkgs.callPackage config.packages.default { })) (getName (baseNixpkgs.callPackage config.packages.default { })) (getName (import inputs.nixpkgs { inherit (prev.stdenv.hostPlatform) system; diff --git a/misc/nameMockedPkgs.nix b/misc/nameMockedPkgs.nix new file mode 100644 index 0000000..8bc0cd3 --- /dev/null +++ b/misc/nameMockedPkgs.nix @@ -0,0 +1,49 @@ +# flakelight -- Framework for simplifying flake setup +# Copyright (C) 2023 Archit Gupta +# SPDX-License-Identifier: MIT + +# This is a fake pkgs set to enable efficiently extracting a derivation's name + +real: +let + inherit (real) lib; + + callPackageWith = autoArgs: fn: args: + let + f = if lib.isFunction fn then fn else import fn; + fargs = lib.functionArgs f; + mock = lib.mapAttrs (_: _: { }) (lib.filterAttrs (_: v: !v) fargs); + in + f (mock // builtins.intersectAttrs fargs autoArgs // args); +in +lib.fix (self: { + pkgs = self; + lib = lib // { inherit callPackageWith; }; + + callPackage = callPackageWith self; + + stdenv = real.stdenv // { + mkDerivation = args: + if lib.isFunction args then lib.fix args else args; + }; + + runCommandWith = { name, ... }: _: { inherit name; }; + runCommand = name: _: _: { inherit name; }; + runCommandLocal = name: _: _: { inherit name; }; + runCommandCC = name: _: _: { inherit name; }; + writeTextFile = { name, ... }: { inherit name; }; + writeText = name: _: { inherit name; }; + writeTextDir = path: _: { name = builtins.baseNameOf path; }; + writeScript = name: _: { inherit name; }; + writeScriptBin = name: _: { inherit name; }; + writeShellScript = name: _: { inherit name; }; + writeShellScriptBin = name: _: { inherit name; }; + writeShellApplication = { name, ... }: { inherit name; }; + writeCBin = pname: _: { inherit pname; }; + concatTextFile = { name, ... }: { inherit name; }; + concatText = name: _: { inherit name; }; + concatScript = name: _: { inherit name; }; + symlinkJoin = { name, ... }: { inherit name; }; + linkFarm = name: _: { inherit name; }; + linkFarmFromDrvs = name: _: { inherit name; }; +})