Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::utils::{CommandExt, ResultExt};
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
pub struct Config {
pub commit: bool,
pub flake: bool,
pub maintainers: Vec<String>,
pub nixpkgs: Option<String>,
pub access_tokens: AccessTokens,
Expand Down
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub struct Opts {
/// Specify the config file
#[arg(short, long)]
pub config: Option<PathBuf>,

/// Generate a flake.nix file in the output directory
#[arg(long)]
pub flake: bool,
}

#[derive(Clone, ValueEnum)]
Expand Down
49 changes: 47 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,8 +1222,8 @@ async fn run() -> Result<()> {
writeln!(out, " }};\n}})")?;

let mut out_file = File::create(&out_path).context("failed to create output file")?;
if let Some(fmt) = cfg.format {
let mut args = fmt.command.into_iter();
if let Some(ref fmt) = cfg.format {
let mut args = fmt.command.iter();
if let Some(cmd) = args.next() {
let mut cmd = Command::new(cmd);
cmd.args(args);
Expand All @@ -1238,6 +1238,51 @@ async fn run() -> Result<()> {
write!(out_file, "{out}")?;
}

if opts.flake || cfg.flake {
if let Some(out_dir) = out_dir {
let flake_path = out_dir.join("flake.nix");
if !flake_path.exists() || frontend.should_overwrite(&flake_path, opts.overwrite)? {
let package_filename = out_path.file_name().unwrap().to_string_lossy();
let flake_content = formatdoc! {r#"
{{
description = {description:?};

inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";

outputs = {{ self, nixpkgs }}: {{
packages = nixpkgs.lib.genAttrs [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
] (system: {{
default = nixpkgs.legacyPackages.${{system}}.callPackage ./{package_filename} {{ }};
}});
}};
}}
"#};
let flake_file =
File::create(&flake_path).context("failed to create flake file")?;
if let Some(ref fmt) = cfg.format {
let mut args = fmt.command.iter();
if let Some(cmd) = args.next() {
let mut cmd_obj = Command::new(cmd);
cmd_obj.args(args);
maybe_format(&flake_content, flake_file, cmd_obj).await?;
} else {
let mut f = flake_file;
write!(f, "{flake_content}")?;
}
} else if which("nixfmt").is_ok() {
maybe_format(&flake_content, flake_file, Command::new("nixfmt")).await?;
} else {
let mut f = flake_file;
write!(f, "{flake_content}")?;
}
}
}
}

if !opts.commit.unwrap_or(cfg.commit) || !Path::new(".git").is_dir() {
return Ok(());
}
Expand Down