crate2nix 0.15.0

crate2nix generates nix (as in NixOS) build files for rust using cargo.
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use std::path::{Path, PathBuf};
use structopt::clap::ArgGroup;
use structopt::StructOpt;

use anyhow::format_err;
use anyhow::{bail, Error};
use crate2nix::{
    config::{Config, NixFile},
    render,
};
use semver::Version;
use serde::Deserialize;
use serde::Serialize;
use std::str::FromStr;

const DEFAULT_OUTPUT: &str = "./Cargo.nix";

#[derive(Debug, StructOpt, Deserialize, Serialize)]
#[structopt(
    name = "crate2nix",
    about = "Nix build file generator for a cargo rust projects."
)]
pub enum Opt {
    #[structopt(
        name = "generate",
        about = "Generates a Cargo.nix file from a cargo rust project."
    )]
    Generate {
        #[structopt(
            short = "c",
            long = "config",
            parse(from_os_str),
            help = "The path to the crate2nix.json file (same directory as Cargo.nix ...).",
            default_value = "./crate2nix.json"
        )]
        crate2nix_json: PathBuf,

        #[structopt(
            short = "f",
            long = "cargo-toml",
            parse(from_os_str),
            help = "The path to the Cargo.toml of the project."
        )]
        cargo_toml: Vec<PathBuf>,

        #[structopt(
            long = "all-features",
            help = "Resolve project dependencies with all features enabled. \
                    This is the default and does not need to be specified. \
                    Users can choose their sub set of features and evaluation time so \
                    that one generated build file can be used for different feature selections."
        )]
        all_features: bool,

        #[structopt(
            long = "default-features",
            help = "Enables the default default features \
                    (instead of all features as is the default). \
                    Often combined with --features to add selected features on top."
        )]
        default_features: bool,

        #[structopt(
            long = "no-default-features",
            help = "Disables all features. \
                    Often combined with --features to reenable selected features."
        )]
        no_default_features: bool,

        #[structopt(
            long = "features",
            help = "Resolve project dependencies additionally with these features enabled. \
                    By default, all features are resolved."
        )]
        features: Vec<String>,

        #[structopt(
            short = "o",
            long = "output",
            help = "The path of the output.nix file. Uses ./Cargo.nix by default."
        )]
        output: Option<PathBuf>,

        #[structopt(
            short = "n",
            long = "nixpkgs-path",
            help = "The default path for the nixpkgs to use.",
            default_value = "<nixpkgs>"
        )]
        nixpkgs_path: String,

        #[structopt(
            short = "h",
            long = "crate-hashes",
            parse(from_os_str),
            help = "The path to the crate hash cache file. \
                    Uses 'crate-hashes.json' in the same directory as the Cargo.nix output by default."
        )]
        crate_hashes: Option<PathBuf>,

        #[structopt(
            short = "r",
            long = "registry-hashes",
            parse(from_os_str),
            help = "The path to the registry hash cache file. \
                    Uses 'registry-hashes.json' in the same directory as the Cargo.nix output by default."
        )]
        registry_hashes: Option<PathBuf>,

        // Mostly useful for testing
        #[structopt(
            long = "no-cargo-lock-checksums",
            help = "(FOR TESTING) Do not use checksums from Cargo.lock."
        )]
        no_cargo_lock_checksums: bool,

        #[structopt(
            long = "dont-read-crate-hashes",
            help = "(FOR TESTING) Do not read crate-hashes file. \
                    If there are any prefetches, their hashes will still be written into crate-hashes.json."
        )]
        dont_read_crate_hashes: bool,
    },

    #[structopt(name = "source", about = "Manage out of tree sources for crate2nix.")]
    Source {
        #[structopt(
            short = "c",
            long = "config",
            parse(from_os_str),
            help = "The path to the crate2nix.json file (same directory as Cargo.nix ...).",
            default_value = "./crate2nix.json"
        )]
        crate2nix_json: PathBuf,

        #[structopt(subcommand)]
        command: SourceCommands,
    },

    #[structopt(
        name = "completions",
        about = "Generates auto-completions for the shell."
    )]
    Completions {
        #[structopt(
            short = "s",
            long = "shell",
            parse(from_str),
            help = "The shell to generate completions for. Specify 'invalid' to get a list of possibilities.",
            default_value = "bash"
        )]
        shell: String,

        #[structopt(
            short = "o",
            long = "output",
            help = "The path of the output directory.",
            default_value = "."
        )]
        output: PathBuf,
    },
}

#[derive(Debug, StructOpt, Deserialize, Serialize)]
#[structopt(about = "Support for managing out-of-tree sources.")]
pub enum SourceCommands {
    #[structopt(name = "add", about = "Adds source, prefetching it if when necessary.")]
    Add {
        #[structopt(subcommand)]
        command: SourceAddingCommands,
    },
    #[structopt(name = "remove", about = "Removes source.")]
    Remove {
        #[structopt(long = "name", help = "The name of the source to remove.")]
        name: String,
    },
    #[structopt(name = "list", about = "Lists all sources.")]
    List,

    #[structopt(
        name = "fetch",
        about = "Fetch all sources with nix.\n\
                 This is usually called automatically and mostly useful for testing."
    )]
    Fetch,
    #[structopt(
        name = "generate",
        about = "Generate crate2nix-sources.nix.\n\
                 This is usually called automatically and mostly useful for testing."
    )]
    Generate,
}

impl SourceCommands {
    pub fn execute(self, crate2nix_json: &Path) -> Result<(), Error> {
        match self {
            SourceCommands::Add { command, .. } => command.execute(crate2nix_json),
            SourceCommands::List => {
                let config = Config::read_from_or_default(crate2nix_json)?;
                config.print_sources();
                Ok(())
            }
            SourceCommands::Remove { name } => {
                let mut config = Config::read_from_or_default(crate2nix_json)?;
                if config.sources.is_empty() {
                    eprintln!(
                        "No sources configured in {}.",
                        crate2nix_json.to_string_lossy()
                    );
                } else {
                    let removed = config.sources.remove(&name);
                    if let Some(removed) = removed {
                        config.write_to(crate2nix_json)?;
                        eprintln!("Removed source\n\t{}", removed);
                    } else {
                        eprintln!("Source '{}' not found among the following sources.\n", name);
                        config.print_sources();
                    }
                }
                Ok(())
            }
            SourceCommands::Fetch => {
                let sources = crate2nix::sources::FetchedSources::new(crate2nix_json);
                let output = sources.fetch()?;
                println!("Fetched sources into {}", output.to_string_lossy());
                Ok(())
            }
            SourceCommands::Generate => {
                let sources = crate2nix::sources::FetchedSources::new(crate2nix_json);
                sources.regenerate_sources_nix()
            }
        }
    }
}

#[derive(Debug, StructOpt, Deserialize, Serialize)]
pub enum SourceAddingCommands {
    #[structopt(name = "cratesIo", about = "Adds source from crates.io.")]
    CratesIo {
        #[structopt(
            long = "name",
            help = "Use this source name instead of the crate name.\n\
                    The source name is used as a workspaceMember name."
        )]
        name: Option<String>,

        #[structopt(help = "The crate name on crates.io.")]
        crate_name: String,

        #[structopt(help = "The full version of the crate.")]
        crate_version: Version,
    },

    #[structopt(
        name = "git",
        about = "Adds git source.\n\
                 \n\
                 If you want auto-update support, consider using the \"nix\" source type\n\
                 and manage the sources with niv.\n\
                 \n\
                 See https://github.com/nmattia/niv."
    )]
    Git {
        #[structopt(
            long = "name",
            help = "Use this source name instead of the last URL path segment without '.git'.\n\
                    The source name is used as a workspaceMember name."
        )]
        name: Option<String>,

        /// The URL of the git repository.
        ///
        /// E.g. https://github.com/kolloch/crate2nix.git
        url: url::Url,

        #[structopt(long = "rev", parse(from_str), help = "The git revision hash.")]
        rev: String,
    },

    #[structopt(
        name = "nix",
        about = "Adds nix attribute from a file as source.\n\
                E.g. crate2nix source add --import nix/sources.nix my_crate.
                 \n\
                 This is the most flexible source type.\n\
                 Works well with tools like niv which support easy updating.",
        // We need either an `--import` or a `--package`.
        group = ArgGroup::with_name("file").required(true),
        // We need an explicit `--name` or an `attr` to derive the name from.
        group = ArgGroup::with_name("some_name").multiple(true).required(true),
    )]
    Nix {
        #[structopt(
            long,
            help = "The name of this source \n\
                    if you do not want to use the last element of the attribute path.",
            group = "some_name"
        )]
        name: Option<String>,

        #[structopt(long, group = "file", help = "A path to `import` in nix.")]
        import: Option<String>,

        #[structopt(
            long,
            group = "file",
            help = "A path to call with `pkgs.callPackage` in nix."
        )]
        package: Option<String>,

        #[structopt(
            help = "The attribute path that leads to the source derivation.",
            group = "some_name"
        )]
        attr: Option<String>,
    },
}

impl SourceAddingCommands {
    pub fn execute(self, crate2nix_json: &Path) -> Result<(), Error> {
        let (name, source) = match self {
            SourceAddingCommands::CratesIo {
                name,
                crate_name,
                crate_version,
            } => {
                let source = crate2nix::sources::crates_io_source(crate_name, crate_version)?;
                (name, source)
            }
            SourceAddingCommands::Git { name, url, rev } => {
                let source = crate2nix::sources::git_io_source(url, rev)?;
                (name, source)
            }
            SourceAddingCommands::Nix {
                name,
                import,
                package,
                attr,
            } => {
                let file = match (import, package) {
                    (Some(import), _) => NixFile::Import(import),
                    (_, Some(package)) => NixFile::Package(package),
                    _ => unreachable!("no file argument given"),
                };

                (name, crate2nix::config::Source::Nix { file, attr })
            }
        };
        let mut config = Config::read_from_or_default(crate2nix_json)?;
        let old_source = config.upsert_source(name, source.clone());
        config.write_to(crate2nix_json)?;
        match old_source {
            Some(old_source) => {
                eprintln!(
                    "Updated existing source\n\t{}\nto\n\t{}",
                    old_source, source
                );
            }
            None => {
                eprintln!("Added new source: {}", source);
            }
        }
        Ok(())
    }
}

fn main() -> anyhow::Result<()> {
    let opt = Opt::from_args();
    match opt {
        Opt::Generate {
            crate2nix_json,
            mut cargo_toml,
            output: opt_output,
            nixpkgs_path,
            crate_hashes,
            registry_hashes,
            all_features,
            default_features,
            no_default_features,
            features,
            no_cargo_lock_checksums,
            dont_read_crate_hashes,
        } => {
            let config = crate2nix::config::Config::read_from_or_default(&crate2nix_json)?;

            if !config.sources.is_empty() {
                let fetched_sources = crate2nix::sources::FetchedSources::new(&crate2nix_json);
                let cargo_tomls = fetched_sources.get_cargo_tomls()?;
                cargo_toml.extend(cargo_tomls);
            }

            if cargo_toml.is_empty() {
                cargo_toml.push("./Cargo.toml".into());
            }

            let output: PathBuf = opt_output
                .map(|v| Ok(v) as Result<_, Error>)
                .unwrap_or_else(|| {
                    crate2nix::render::check_generated_by_crate2nix(DEFAULT_OUTPUT)?;
                    Ok(DEFAULT_OUTPUT.into())
                })?;

            let crate_hashes_json = crate_hashes.unwrap_or_else(|| {
                output
                    .parent()
                    .expect("Cargo.nix has parent")
                    .join("crate-hashes.json")
            });

            let registry_hashes_json = registry_hashes.unwrap_or_else(|| {
                output
                    .parent()
                    .expect("Cargo.nix has parent")
                    .join("registry-hashes.json")
            });

            let generate_info = crate2nix::GenerateInfo::default();

            let feature_metadata_options = || {
                let mut options = Vec::new();

                if [all_features, default_features, no_default_features]
                    .iter()
                    .filter(|x| **x)
                    .count()
                    > 1
                {
                    bail!(
                        "Please specify at most one of \
                         --all-features, --no-default-features and --default-features."
                    )
                }

                // "cargo metadata" will default to the "default features".
                // crate2nix defaults to "--all-features" since this allows users to choose
                // any set of features at evaluation time.
                let all_features = !no_default_features && !default_features;
                if no_default_features {
                    options.push("--no-default-features".to_string());
                } else if !default_features {
                    assert!(all_features);
                    options.push("--all-features".to_string());
                }

                if !features.is_empty() {
                    if all_features {
                        bail!(
                            "You specified --features but --all-features was already selected. \
                               Use --no-default-features or --default-features to only select \
                               some features as a basis and then use --features to add additional \
                               features on top."
                        )
                    }
                    options.push("--features".to_string());
                    options.push(features.join(" "));
                }

                Ok(options)
            };

            let generate_config = crate2nix::GenerateConfig {
                cargo_toml,
                output: output.clone(),
                nixpkgs_path,
                crate_hashes_json,
                registry_hashes_json,
                other_metadata_options: feature_metadata_options()?,
                use_cargo_lock_checksums: !no_cargo_lock_checksums,
                read_crate_hashes: !dont_read_crate_hashes,
            };
            let build_info = crate2nix::BuildInfo::for_config(&generate_info, &generate_config)?;
            render::CARGO_NIX.write_to_file(&output, &build_info)?;
        }
        Opt::Completions { shell, output } => {
            let shell = FromStr::from_str(&shell).map_err(|s| format_err!("{}", s))?;
            Opt::clap().gen_completions(env!("CARGO_PKG_NAME"), shell, output);
        }
        Opt::Source {
            crate2nix_json,
            command,
        } => {
            command.execute(&crate2nix_json)?;
        }
    }

    Ok(())
}