@@ -68,23 +68,24 @@ fn create_gcroot(gcroot_path: &str, profile_path: &Path) -> Result<()> {
6868 create_store_link ( & store_path, Path :: new ( gcroot_path) )
6969}
7070
71- pub fn build ( nix_build_options : & NixBuildOptions , nix_options : & NixOptions ) -> Result < StorePath > {
72- let full_flake_uri = find_flake_attr ( & nix_build_options. flake_uri , nix_options) ?;
71+ pub fn build (
72+ nix_build_options : & mut NixBuildOptions ,
73+ nix_options : & NixOptions ,
74+ ) -> Result < StorePath > {
75+ nix_build_options. flake_uri = find_flake_attr ( nix_build_options, nix_options) ?;
7376
7477 log:: info!( "Building new system-manager generation..." ) ;
7578 log:: info!( "Running nix build..." ) ;
76- let store_path = run_nix_build (
77- full_flake_uri. as_ref ( ) ,
78- nix_options,
79- nix_build_options. refresh ,
80- )
81- . and_then ( get_store_path) ?;
79+ let store_path = run_nix_build ( nix_build_options, nix_options) . and_then ( get_store_path) ?;
8280 log:: info!( "Built system-manager profile {store_path}" ) ;
8381 Ok ( store_path)
8482}
8583
86- fn find_flake_attr ( flake_uri : & str , nix_options : & NixOptions ) -> Result < String > {
87- let flake_uri = flake_uri. trim_end_matches ( '#' ) ;
84+ fn find_flake_attr (
85+ nix_build_options : & NixBuildOptions ,
86+ nix_options : & NixOptions ,
87+ ) -> Result < String > {
88+ let flake_uri = nix_build_options. flake_uri . trim_end_matches ( '#' ) ;
8889 let mut splitted = flake_uri. split ( '#' ) ;
8990 let flake = splitted
9091 . next ( )
@@ -98,7 +99,9 @@ fn find_flake_attr(flake_uri: &str, nix_options: &NixOptions) -> Result<String>
9899 let system = get_nix_system ( nix_options) ?;
99100
100101 if let Some ( attr) = attr {
101- let Some ( full_uri) = try_flake_attr ( flake, attr, nix_options, & system) ? else {
102+ let Some ( full_uri) =
103+ try_flake_attr ( flake, attr, nix_options, & system, nix_build_options. refresh ) ?
104+ else {
102105 anyhow:: bail!(
103106 "Explicitly provided flake URI does not point to a valid system-manager configuration: {flake}#{attr}"
104107 )
@@ -110,9 +113,21 @@ fn find_flake_attr(flake_uri: &str, nix_options: &NixOptions) -> Result<String>
110113 let hostname = escape_nix_string ( & hostname_os. to_string_lossy ( ) ) ;
111114 let default = "default" ;
112115
113- if let Some ( full_uri) = try_flake_attr ( flake, & hostname, nix_options, & system) ? {
116+ if let Some ( full_uri) = try_flake_attr (
117+ flake,
118+ & hostname,
119+ nix_options,
120+ & system,
121+ nix_build_options. refresh ,
122+ ) ? {
114123 return Ok ( full_uri) ;
115- } else if let Some ( full_uri) = try_flake_attr ( flake, default, nix_options, & system) ? {
124+ } else if let Some ( full_uri) = try_flake_attr (
125+ flake,
126+ default,
127+ nix_options,
128+ & system,
129+ nix_build_options. refresh ,
130+ ) ? {
116131 return Ok ( full_uri) ;
117132 } ;
118133 anyhow:: bail!( "No suitable flake attribute found, giving up." ) ;
@@ -136,11 +151,12 @@ fn try_flake_attr(
136151 attr : & str ,
137152 nix_options : & NixOptions ,
138153 system : & str ,
154+ refresh : bool ,
139155) -> Result < Option < String > > {
140156 let try_flake_attr_impl = |attr : & str | {
141157 let full_uri = format ! ( "{flake}#{FLAKE_ATTR}.{attr}" ) ;
142158 log:: info!( "Trying flake URI: {full_uri}..." ) ;
143- let status = try_nix_eval ( flake, attr, nix_options) ?;
159+ let status = try_nix_eval ( flake, attr, nix_options, refresh ) ?;
144160 if status {
145161 log:: info!( "Success, using {full_uri}" ) ;
146162 Ok ( Some ( full_uri) )
@@ -182,13 +198,14 @@ fn parse_nix_build_output(output: String) -> Result<StorePath> {
182198}
183199
184200fn run_nix_build (
185- flake_uri : & str ,
201+ nix_build_options : & NixBuildOptions ,
186202 nix_options : & NixOptions ,
187- refresh : bool ,
188203) -> Result < process:: Output > {
189204 let mut cmd = nix_cmd ( nix_options) ;
190- cmd. arg ( "build" ) . arg ( flake_uri) . arg ( "--json" ) ;
191- if refresh {
205+ cmd. arg ( "build" )
206+ . arg ( & nix_build_options. flake_uri )
207+ . arg ( "--json" ) ;
208+ if nix_build_options. refresh {
192209 cmd. arg ( "--refresh" ) ;
193210 }
194211
@@ -203,13 +220,16 @@ fn run_nix_build(
203220 Ok ( output)
204221}
205222
206- fn try_nix_eval ( flake : & str , attr : & str , nix_options : & NixOptions ) -> Result < bool > {
223+ fn try_nix_eval ( flake : & str , attr : & str , nix_options : & NixOptions , refresh : bool ) -> Result < bool > {
207224 let mut cmd = nix_cmd ( nix_options) ;
208225 cmd. arg ( "eval" )
209226 . arg ( format ! ( "{flake}#{FLAKE_ATTR}" ) )
210227 . arg ( "--json" )
211228 . arg ( "--apply" )
212229 . arg ( format ! ( "_: _ ? {attr}" ) ) ;
230+ if refresh {
231+ cmd. arg ( "--refresh" ) ;
232+ }
213233
214234 log:: debug!( "Running nix command: {cmd:?}" ) ;
215235
@@ -262,9 +282,9 @@ mod tests {
262282 let flake = "./test/rust/register" ;
263283 let nix_options = & NixOptions :: new ( vec ! [ ] ) ;
264284
265- assert ! ( try_nix_eval( flake, "identifier-key" , nix_options) . unwrap( ) ) ;
266- assert ! ( try_nix_eval( flake, "\" string.literal/key\" " , nix_options) . unwrap( ) ) ;
267- assert ! ( !try_nix_eval( flake, "_identifier-key" , nix_options) . unwrap( ) ) ;
268- assert ! ( !try_nix_eval( flake, "\" _string.literal/key\" " , nix_options) . unwrap( ) ) ;
285+ assert ! ( try_nix_eval( flake, "identifier-key" , nix_options, false ) . unwrap( ) ) ;
286+ assert ! ( try_nix_eval( flake, "\" string.literal/key\" " , nix_options, false ) . unwrap( ) ) ;
287+ assert ! ( !try_nix_eval( flake, "_identifier-key" , nix_options, false ) . unwrap( ) ) ;
288+ assert ! ( !try_nix_eval( flake, "\" _string.literal/key\" " , nix_options, false ) . unwrap( ) ) ;
269289 }
270290}
0 commit comments