nix-index 0.1.10

Nix (package manager) indexing primitives
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Read package information from nix-env.
//!
//! This module implements the gathering of initial set of root store paths to fetch.
//! We parse the output `nix-env --query` to figure out all accessible store paths with their attribute path
//! and hashes.
use std::error;
use std::fmt;
use std::io::{self, Read};
use std::process::{Child, ChildStdout, Command, Stdio};

use xml;
use xml::common::{Position, TextPosition};
use xml::reader::{EventReader, XmlEvent};

use crate::package::{PathOrigin, StorePath};

/// Calls `nix-env` to list the packages in the given nixpkgs.
///
/// The `nixpkgs` argument can either be a path to a nixpkgs checkout or another expression
/// accepted by `nix-env -f`, such as `<nixpkgs>` or `http://example.org/nixpkgs.tar.bz`.
///
/// If system is `Some(platform)`, nix-env is called with the `--argstr system <platform>` argument so that
/// the specified platform would be used instead of the default host system platform.
///
/// If scope is `Some(attr)`, nix-env is called with the `-A attr` argument so only packages that are a member
/// of `attr` are returned.
///
/// The function returns an Iterator over the packages returned by nix-env.
pub fn query_packages(
    nixpkgs: &str,
    system: Option<&str>,
    scope: Option<&str>,
    show_trace: bool,
) -> PackagesQuery<ChildStdout> {
    let mut cmd = Command::new("nix-env");
    cmd.arg("-qaP")
        .arg("--out-path")
        .arg("--xml")
        .arg("--arg")
        .arg("config")
        .arg("{ allowAliases = false; }") // override default nixpkgs config discovery
        .arg("--arg")
        .arg("overlays")
        .arg("[ ]")
        .arg("--file")
        .arg(nixpkgs)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .stdin(Stdio::null());

    if let Some(system) = system {
        cmd.arg("--argstr").arg("system").arg(system);
    }

    if let Some(scope) = scope {
        cmd.arg("-A").arg(scope);
    }

    if show_trace {
        cmd.arg("--show-trace");
    }

    PackagesQuery {
        parser: None,
        child: None,
        cmd: Some(cmd),
    }
}

/// An iterator that parses the output of nix-env and returns parsed store paths.
///
/// Use `query_packages` to create a value of this type.
pub struct PackagesQuery<R: Read> {
    parser: Option<PackagesParser<R>>,
    child: Option<Child>,
    cmd: Option<Command>,
}

impl PackagesQuery<ChildStdout> {
    /// Spawns the nix-env subprocess and initializes the parser.
    ///
    /// If the subprocess was already spawned, does nothing.
    fn ensure_initialized(&mut self) -> Result<(), Error> {
        if let Some(mut cmd) = self.cmd.take() {
            let mut child = cmd.spawn()?;

            let stdout = child.stdout.take().expect("should have stdout pipe");
            let parser = PackagesParser::new(stdout);

            self.child = Some(child);
            self.parser = Some(parser);
        }
        Ok(())
    }

    /// Waits for the subprocess to exit and checks whether it has returned a non-zero exit code
    /// (= failed with an error).
    ///
    /// If the exit code was non-zero, returns Some(err), else it returns None.
    fn check_error(&mut self) -> Option<Error> {
        let mut run = || {
            let child = match self.child.take() {
                Some(c) => c,
                None => return Ok(()),
            };
            let result = child.wait_with_output()?;

            if !result.status.success() {
                let message = String::from_utf8_lossy(&result.stderr);

                return Err(Error::Command(format!(
                    "nix-env failed with {}:\n{}",
                    result.status, message,
                )));
            }

            Ok(())
        };

        run().err()
    }
}

impl Iterator for PackagesQuery<ChildStdout> {
    type Item = Result<StorePath, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Err(e) = self.ensure_initialized() {
            return Some(Err(e));
        }
        self.parser.take().and_then(|mut parser| {
            parser
                .next()
                .map(|v| {
                    self.parser = Some(parser);
                    // When the parser throws an error, we first wait for the subprocess to exit.
                    //
                    // If the subprocess returned an error, then the parser probably tried to parse garbage output
                    // so we will ignore the parser error and instead return the error printed by the subprocess.
                    v.map_err(|e| self.check_error().unwrap_or_else(|| Error::from(e)))
                })
                .or_else(|| {
                    self.parser = None;
                    // At the end, we should check if the subprocess exited successfully.
                    self.check_error().map(Err)
                })
        })
    }
}

/// Parses the XML output of `nix-env` and returns individual store paths.
struct PackagesParser<R: Read> {
    events: EventReader<R>,
    current_item: Option<(String, String)>,
}

/// A parser error that may occur during parsing `nix-env`'s output.
#[derive(Debug)]
pub struct ParserError {
    position: TextPosition,
    kind: ParserErrorKind,
}

/// Enumerates all possible error kinds that may occur during parsing.
#[derive(Debug)]
pub enum ParserErrorKind {
    /// Found an element with the tag `element_name` that should only occur inside
    /// elements with the tag `expected_parent` but it occurred as child of a different parent.
    MissingParent {
        element_name: String,
        expected_parent: String,
    },

    /// An element occurred as a child of `found_parent`, but
    /// we know that elements with the tag `element_name` should never have that as
    /// a parent.
    ParentNotAllowed {
        element_name: String,
        found_parent: String,
    },

    /// The required attribute `attribute_name` was missing on an element with the tag `element_name`.
    MissingAttribute {
        element_name: String,
        attribute_name: String,
    },

    /// Found the end tag for `element_name` without a matching start tag.
    MissingStartTag { element_name: String },

    /// An XML syntax error.
    XmlError { error: xml::reader::Error },

    /// A store path in the output of `nix-env` could not be parsed. All valid store paths
    /// need to match the format `$(STOREDIR)$(HASH)-$(NAME)`.
    InvalidStorePath { path: String },
}

impl fmt::Display for ParserError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use self::ParserErrorKind::*;
        write!(f, "error at {}: ", self.position)?;
        match self.kind {
            MissingParent {
                ref element_name,
                ref expected_parent,
            } => {
                write!(
                    f,
                    "element {} appears outside of expected parent {}",
                    element_name, expected_parent
                )
            }
            ParentNotAllowed {
                ref element_name,
                ref found_parent,
            } => {
                write!(
                    f,
                    "element {} must not appear as child of {}",
                    element_name, found_parent
                )
            }
            MissingAttribute {
                ref element_name,
                ref attribute_name,
            } => {
                write!(
                    f,
                    "element {} must have an attribute named {}",
                    element_name, attribute_name
                )
            }
            MissingStartTag { ref element_name } => {
                write!(f, "element {} does not have a start tag", element_name)
            }
            XmlError { ref error } => write!(f, "document not well-formed: {}", error),
            InvalidStorePath { ref path } => {
                write!(
                    f,
                    "store path does not match expected format /prefix/hash-name: {}",
                    path
                )
            }
        }
    }
}

impl<R: Read> PackagesParser<R> {
    /// Creates a new parser that reads the `nix-env` XML output from the given reader.
    pub fn new(reader: R) -> PackagesParser<R> {
        PackagesParser {
            events: EventReader::new(reader),
            current_item: None,
        }
    }

    /// Shorthand for exiting with an error at the current position.
    fn err(&self, kind: ParserErrorKind) -> ParserError {
        ParserError {
            position: self.events.position(),
            kind,
        }
    }

    /// Tries to read the next `StorePath` from the reader or fail with an error
    /// if there was a parse failure.
    ///
    /// Returns Ok(None) if the end of the stream was reached.
    ///
    /// This function is like `.next` from `Iterator`, but allows us to use `try! / ?` since it
    /// returns `Result<Option<...>, ...>` instead of `Option<Result<..., ...>>`.
    fn next_err(&mut self) -> Result<Option<StorePath>, ParserError> {
        use self::ParserErrorKind::*;
        use self::XmlEvent::*;

        loop {
            let event = self
                .events
                .next()
                .map_err(|e| self.err(XmlError { error: e }))?;
            match event {
                StartElement {
                    name: element_name,
                    attributes,
                    ..
                } => {
                    if element_name.local_name == "item" {
                        if self.current_item.is_some() {
                            return Err(self.err(ParentNotAllowed {
                                element_name: "item".to_string(),
                                found_parent: "item".to_string(),
                            }));
                        }

                        let mut attr_path = None;
                        let mut system = None;

                        for attr in attributes {
                            if attr.name.local_name == "attrPath" {
                                attr_path = Some(attr.value);
                                continue;
                            }

                            if attr.name.local_name == "system" {
                                system = Some(attr.value);
                                continue;
                            }
                        }

                        let attr_path = attr_path.ok_or_else(|| {
                            self.err(MissingAttribute {
                                element_name: "item".into(),
                                attribute_name: "attrPath".into(),
                            })
                        })?;

                        let system = system.ok_or_else(|| {
                            self.err(MissingAttribute {
                                element_name: "item".into(),
                                attribute_name: "system".into(),
                            })
                        })?;

                        self.current_item = Some((attr_path, system));
                        continue;
                    }

                    if element_name.local_name == "output" {
                        if let Some((item, system)) = self.current_item.clone() {
                            let mut output_name = None;
                            let mut output_path = None;

                            for attr in attributes {
                                if attr.name.local_name == "name" {
                                    output_name = Some(attr.value);
                                    continue;
                                }

                                if attr.name.local_name == "path" {
                                    output_path = Some(attr.value);
                                    continue;
                                }
                            }

                            let output_name = output_name.ok_or_else(|| {
                                self.err(MissingAttribute {
                                    element_name: "output".into(),
                                    attribute_name: "name".into(),
                                })
                            })?;

                            let output_path = output_path.ok_or_else(|| {
                                self.err(MissingAttribute {
                                    element_name: "output".into(),
                                    attribute_name: "path".into(),
                                })
                            })?;

                            let origin = PathOrigin {
                                attr: item,
                                output: output_name,
                                toplevel: true,
                                system: Some(system),
                            };
                            let store_path = StorePath::parse(origin, &output_path);
                            let store_path = store_path
                                .ok_or_else(|| self.err(InvalidStorePath { path: output_path }))?;

                            return Ok(Some(store_path));
                        } else {
                            return Err(self.err(MissingParent {
                                element_name: "output".into(),
                                expected_parent: "item".into(),
                            }));
                        }
                    }
                }

                EndElement { name: element_name } => {
                    if element_name.local_name == "item" {
                        if self.current_item.is_none() {
                            return Err(self.err(MissingStartTag {
                                element_name: "item".into(),
                            }));
                        }
                        self.current_item = None
                    }
                }

                EndDocument => break,

                _ => {}
            }
        }

        Ok(None)
    }
}

impl<R: Read> Iterator for PackagesParser<R> {
    type Item = Result<StorePath, ParserError>;

    fn next(&mut self) -> Option<Result<StorePath, ParserError>> {
        match self.next_err() {
            Err(e) => Some(Err(e)),
            Ok(Some(i)) => Some(Ok(i)),
            Ok(None) => None,
        }
    }
}

/// Enumeration of all the possible errors that may happen during querying the packages.
#[derive(Debug)]
pub enum Error {
    /// Parsing of the output failed
    Parse(ParserError),

    /// An IO error occurred
    Io(io::Error),

    /// nix-env failed with an error message
    Command(String),
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Parse(_) => "nix-env output parse error",
            Error::Io(_) => "io error",
            Error::Command(_) => "nix-env error",
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use self::Error::*;
        match *self {
            Parse(ref e) => write!(f, "parsing XML output of nix-env failed: {}", e),
            Io(ref e) => write!(f, "IO error: {}", e),
            Command(ref e) => write!(f, "nix-env failed with error: {}", e),
        }
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::Io(err)
    }
}

impl From<ParserError> for Error {
    fn from(err: ParserError) -> Error {
        Error::Parse(err)
    }
}