@@ -4,7 +4,7 @@ use std::collections::BTreeMap;
4
4
5
5
use nix_rs:: {
6
6
command:: NixCmd ,
7
- flake:: { eval:: nix_eval_attr, url:: FlakeUrl } ,
7
+ flake:: { eval:: nix_eval_attr, metadata :: FlakeMetadata , url:: FlakeUrl } ,
8
8
} ;
9
9
use serde:: { de:: DeserializeOwned , Deserialize } ;
10
10
@@ -24,12 +24,44 @@ pub struct OmConfig {
24
24
}
25
25
26
26
impl OmConfig {
27
- /// Read the om configuration from the flake url
28
- pub async fn from_flake_url ( cmd : & NixCmd , flake_url : & FlakeUrl ) -> Result < Self , OmConfigError > {
27
+ /// Fetch the `om` configuration from `om.yaml` if present, falling back to `om` config in flake output
28
+ pub async fn get ( flake_url : & FlakeUrl ) -> Result < Self , OmConfigError > {
29
+ match Self :: from_yaml ( flake_url) . await ? {
30
+ None => Self :: from_flake ( flake_url) . await ,
31
+ Some ( config) => Ok ( config) ,
32
+ }
33
+ }
34
+
35
+ /// Read the configuration from `om.yaml` in flake root
36
+ async fn from_yaml ( flake_url : & FlakeUrl ) -> Result < Option < Self > , OmConfigError > {
37
+ let path = if let Some ( local_path) = flake_url. without_attr ( ) . as_local_path ( ) {
38
+ local_path. to_path_buf ( )
39
+ } else {
40
+ FlakeMetadata :: from_nix ( NixCmd :: get ( ) . await , flake_url)
41
+ . await ?
42
+ . path
43
+ }
44
+ . join ( "om.yaml" ) ;
45
+
46
+ if !path. exists ( ) {
47
+ return Ok ( None ) ;
48
+ }
49
+
50
+ let yaml_str = std:: fs:: read_to_string ( path) ?;
51
+ let config: OmConfigTree = serde_yaml:: from_str ( & yaml_str) ?;
52
+ Ok ( Some ( OmConfig {
53
+ flake_url : flake_url. without_attr ( ) ,
54
+ reference : flake_url. get_attr ( ) . as_list ( ) ,
55
+ config,
56
+ } ) )
57
+ }
58
+
59
+ /// Read the configuration from `om` flake output
60
+ async fn from_flake ( flake_url : & FlakeUrl ) -> Result < Self , OmConfigError > {
29
61
Ok ( OmConfig {
30
62
flake_url : flake_url. without_attr ( ) ,
31
63
reference : flake_url. get_attr ( ) . as_list ( ) ,
32
- config : nix_eval_attr ( cmd , & flake_url. with_attr ( "om" ) )
64
+ config : nix_eval_attr ( NixCmd :: get ( ) . await , & flake_url. with_attr ( "om" ) )
33
65
. await ?
34
66
. unwrap_or_default ( ) ,
35
67
} )
@@ -108,4 +140,12 @@ pub enum OmConfigError {
108
140
/// Failed to parse JSON
109
141
#[ error( "Failed to decode (json error): {0}" ) ]
110
142
DecodeErrorJson ( #[ from] serde_json:: Error ) ,
143
+
144
+ /// Failed to parse yaml
145
+ #[ error( "Failed to parse yaml: {0}" ) ]
146
+ ParseYaml ( #[ from] serde_yaml:: Error ) ,
147
+
148
+ /// Failed to read yaml
149
+ #[ error( "Failed to read yaml: {0}" ) ]
150
+ ReadYaml ( #[ from] std:: io:: Error ) ,
111
151
}
0 commit comments