@@ -49,12 +49,12 @@ func StorePathsFromInstallable(ctx context.Context, installable string, allowIns
49
49
return nil , err
50
50
}
51
51
52
- validPaths , err := parseStorePathFromInstallableOutput (resultBytes )
52
+ paths , err := parseStorePathFromInstallableOutput (resultBytes )
53
53
if err != nil {
54
54
return nil , fmt .Errorf ("failed to parse path-info for %s: %w" , installable , err )
55
55
}
56
56
57
- return maps .Keys (validPaths ), nil
57
+ return maps .Keys (paths ), nil
58
58
}
59
59
60
60
// StorePathsAreInStore a map of store paths to whether they are in the store.
@@ -71,44 +71,39 @@ func StorePathsAreInStore(ctx context.Context, storePaths []string) (map[string]
71
71
return nil , err
72
72
}
73
73
74
- validPaths , err := parseStorePathFromInstallableOutput (output )
75
- if err != nil {
76
- return nil , err
77
- }
78
-
79
- result := map [string ]bool {}
80
- for _ , storePath := range storePaths {
81
- _ , ok := validPaths [storePath ]
82
- result [storePath ] = ok
83
- }
84
-
85
- return result , nil
74
+ return parseStorePathFromInstallableOutput (output )
86
75
}
87
76
88
77
// Older nix versions (like 2.17) are an array of objects that contain path and valid fields
89
- type pathInfoLegacy struct {
90
- Path string `json:"path"`
78
+ type LegacyPathInfo struct {
79
+ Path string `json:"path"`
80
+ Valid bool `json:"valid"` // this means path is in store
91
81
}
92
82
93
83
// parseStorePathFromInstallableOutput parses the output of `nix store path-from-installable --json`
84
+ // into a map of store paths to whether they are in the store.
94
85
// This function is decomposed out of StorePathFromInstallable to make it testable.
95
- func parseStorePathFromInstallableOutput (output []byte ) (map [string ]any , error ) {
86
+ func parseStorePathFromInstallableOutput (output []byte ) (map [string ]bool , error ) {
87
+ result := map [string ]bool {}
88
+
96
89
// Newer nix versions (like 2.20) have output of the form
97
90
// {"<store-path>": {}}
98
91
// Note that values will be null if paths are not in store.
99
- var out1 map [string ]any
100
- if err := json .Unmarshal (output , & out1 ); err == nil {
101
- return out1 , nil
92
+ var modernPathInfo map [string ]any
93
+ if err := json .Unmarshal (output , & modernPathInfo ); err == nil {
94
+ for path , val := range modernPathInfo {
95
+ result [path ] = val != nil
96
+ }
97
+ return result , nil
102
98
}
103
99
104
- var out2 []pathInfoLegacy
100
+ var legacyPathInfos []LegacyPathInfo
105
101
106
- if err := json .Unmarshal (output , & out2 ); err == nil {
107
- res := map [string ]any {}
108
- for _ , outValue := range out2 {
109
- res [outValue .Path ] = true
102
+ if err := json .Unmarshal (output , & legacyPathInfos ); err == nil {
103
+ for _ , outValue := range legacyPathInfos {
104
+ result [outValue .Path ] = outValue .Valid
110
105
}
111
- return res , nil
106
+ return result , nil
112
107
}
113
108
114
109
return nil , fmt .Errorf ("failed to parse path-info output: %s" , output )
0 commit comments