44import os
55from pathlib import Path
66from tempfile import NamedTemporaryFile
7- from typing import TYPE_CHECKING
7+ from typing import TYPE_CHECKING , Self
88
99from .utils import die
1010
@@ -23,39 +23,51 @@ def find_nixpkgs_root() -> Path | None:
2323
2424
2525class Buildenv :
26- def __init__ (self , allow_aliases : bool , extra_nixpkgs_config : str ) -> None :
26+ def __init__ (
27+ self , allow_aliases : bool , extra_nixpkgs_config : str , extra_nixpkgs_args : str
28+ ) -> None :
2729 if not (
2830 extra_nixpkgs_config .startswith ("{" ) and extra_nixpkgs_config .endswith ("}" )
2931 ):
3032 msg = "--extra-nixpkgs-config must start with `{` and end with `}`"
3133 raise RuntimeError (msg )
34+ if not (
35+ extra_nixpkgs_args .startswith ("{" ) and extra_nixpkgs_args .endswith ("}" )
36+ ):
37+ msg = "--extra-nixpkgs-args must start with `{` and end with `}`"
38+ raise RuntimeError (msg )
3239
33- self .nixpkgs_config = NamedTemporaryFile (suffix = ".nix" ) # noqa: SIM115
40+ self ._nixpkgs_wrapper_file = NamedTemporaryFile (suffix = ".nix" ) # noqa: SIM115
3441 self .old_cwd : Path | None = None
3542 self .environ : dict [str , str ] | None = None
3643 aliases_config = "allowAliases = false;" if not allow_aliases else ""
37- config_content = f"""{{
38- allowUnfree = true;
39- allowBroken = true;
40- { aliases_config }
41- checkMeta = true;
42- ## TODO: also build packages marked as insecure
43- # allowInsecurePredicate = x: true;
44- }} // { extra_nixpkgs_config }
44+ config_content = f"""{{...}}@args:
45+ let extraArgs = { extra_nixpkgs_args } ;
46+ in import <nixpkgs> ({{
47+ config = {{
48+ allowUnfree = true;
49+ allowBroken = true;
50+ { aliases_config }
51+ checkMeta = true;
52+ ## TODO: also build packages marked as insecure
53+ # allowInsecurePredicate = x: true;
54+ }} // { extra_nixpkgs_config } // extraArgs.config or {{}} // args.config or {{}};
55+ }} // extraArgs // args)
4556"""
46- self .nixpkgs_config .write (config_content .encode ())
47- self .nixpkgs_config .flush ()
57+ self ._nixpkgs_wrapper_file .write (config_content .encode ())
58+ self ._nixpkgs_wrapper_file .flush ()
4859
49- def __enter__ (self ) -> Path :
60+ def __enter__ (self ) -> Self :
5061 self .environ = os .environ .copy ()
5162 self .old_cwd = Path .cwd ()
63+ self .nixpkgs_wrapper = Path (self ._nixpkgs_wrapper_file .name )
5264
5365 if (root := find_nixpkgs_root ()) is None :
5466 die ("Has to be executed from nixpkgs repository" )
5567 os .chdir (root )
5668
57- os .environ ["NIXPKGS_CONFIG" ] = self . nixpkgs_config . name
58- return Path ( self . nixpkgs_config . name )
69+ os .environ ["NIXPKGS_CONFIG" ] = ""
70+ return self
5971
6072 def __exit__ (
6173 self ,
@@ -71,5 +83,5 @@ def __exit__(
7183 os .environ .clear ()
7284 os .environ .update (self .environ )
7385
74- if self .nixpkgs_config is not None :
75- self .nixpkgs_config .close ()
86+ if self ._nixpkgs_wrapper_file is not None :
87+ self ._nixpkgs_wrapper_file .close ()
0 commit comments