Skip to content

Commit 6d259a3

Browse files
authored
Merge pull request #62 from nix-community/nixfmt-auto-root-flag
- Make sure generated `bun.nix` files follow `nixfmt` conventions - Add a flag for overriding the path to copy workspace packages from;
2 parents f791ae9 + b2d6a93 commit 6d259a3

File tree

26 files changed

+119
-43
lines changed

26 files changed

+119
-43
lines changed

docs/src/using-the-command-line-tool.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Usage
4040
Options
4141
-l, --lock-file The Bun (v1.2+) lockfile to use to produce the Nix expression (default bun.lock)
4242
-o, --output-file The output file to write to - if no file location is provided, print to stdout instead
43+
-c, --copy-prefix The prefix to use when copying workspace or file packages (default ./)
4344
-v, --version Displays current version
4445
-h, --help Displays this message
4546
```
@@ -56,6 +57,7 @@ Usage: bun2nix [OPTIONS]
5657
Options:
5758
-l, --lock-file <LOCK_FILE> The Bun (v1.2+) lockfile to use to produce the Nix expression [default: ./bun.lock]
5859
-o, --output-file <OUTPUT_FILE> The output file to write to - if no file location is provided, print to stdout instead
60+
-c, --copy-prefix <COPY_PREFIX> The prefix to use when copying workspace or file packages [default: ./]
5961
-h, --help Print help
6062
-V, --version Print version
6163
```

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nix/checks/arbitrary-install-completes.nix

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
checks.arbitraryInstallCompletes = pkgs.stdenv.mkDerivation {
77
name = "bun2nix-exec-test";
88

9-
outputHash = "sha256-xOyFWDXEhcpw/36e888JC+1vwNm5o+O3JpZfTfSsg1I=";
9+
outputHash = "sha256-rMTHsppsDkgog5w8dJ5w51vU1F8XvdzAbtXXacK4Rx4=";
1010
outputHashAlgo = "sha256";
1111
outputHashMode = "recursive";
1212

@@ -16,6 +16,7 @@
1616
nix
1717
cacert
1818
git
19+
nixfmt
1920
];
2021

2122
installPhase = ''
@@ -28,11 +29,18 @@
2829
export HOME=$PWD/home
2930
mkdir -p $NIX_STATE_DIR $NIX_STORE_DIR $NIX_PROFILES_DIR $NIX_CONF_DIR $HOME
3031
32+
bun_nix=$(${lib.getExe self'.packages.bun2nix})
33+
3134
nix eval \
3235
--extra-experimental-features nix-command \
33-
--expr "$(${lib.getExe self'.packages.bun2nix})"
36+
--expr "$bun_nix"
37+
38+
echo ${self'.packages.bun2nix.version} > "$out"
3439
35-
echo ${self'.packages.bun2nix.version} > $out
40+
if [ "$bun_nix" != "$(echo "$bun_nix" | nixfmt)" ]; then
41+
printf '\n\033[31mError:\033[0m %s\n\n' "bun2nix generated file should be nix formatted"
42+
exit 1
43+
fi
3644
'';
3745
};
3846
};

programs/bun2nix/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

programs/bun2nix/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ path = "src/lib.rs"
2121

2222
[package]
2323
name = "bun2nix"
24-
version = "2.0.4"
24+
version = "2.0.5"
2525
edition = "2021"
2626

2727
[profile.release]

programs/bun2nix/index.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
#!/usr/bin/env bun
22

3-
import { convert_lockfile_to_nix_expression } from "./bun2nix-wasm.js";
3+
import { convert_lockfile_to_nix_expression, Options } from "./bun2nix-wasm.js";
44

55
import sade from "sade";
66
import pkgJson from "./package.json" with { type: "json" };
77

88
const prog = sade("bun2nix", true);
99

1010
/** `bun2nix` command line options. */
11-
type Opts = {
11+
type CliOpts = {
1212
/** String path to the lockfile to read in. */
1313
"lock-file": string;
1414
/** Output file to write to - writes to stdout if undefined. */
1515
"output-file": string | undefined;
16+
/** The prefix to use when copying workspace or file packages. */
17+
"copy-prefix": string;
1618
};
1719

1820
/**
1921
* Generate a nix expression for a given bun lockfile
2022
* Writes to stdout if `output-file` is not specified.
2123
*
22-
* @param {Opts} opts - An instance of bun2nix CLI options
24+
* @param {CliOpts} opts - An instance of bun2nix CLI options
2325
*/
24-
export async function generateNixExpression(opts: Opts): Promise<void> {
26+
export async function generateNixExpression(opts: CliOpts): Promise<void> {
2527
const lock_file = Bun.file(opts["lock-file"]);
2628
const contents = await lock_file.text();
2729

28-
const nix_expression = convert_lockfile_to_nix_expression(contents);
30+
const options = new Options(opts["copy-prefix"]);
31+
32+
const nix_expression = convertLockfileToNixExpression(contents, options);
2933

3034
const output_file = opts["output-file"] || Bun.stdout;
3135
await Bun.write(output_file, nix_expression + "\n");
@@ -43,6 +47,11 @@ prog
4347
"-o, --output-file",
4448
"The output file to write to - if no file location is provided, print to stdout instead",
4549
)
50+
.option(
51+
"-c, --copy-prefix",
52+
"The prefix to use when copying workspace or file packages",
53+
"./",
54+
)
4655
.action((opts) => generateNixExpression(opts));
4756

4857
prog.parse(process.argv);
@@ -51,8 +60,12 @@ prog.parse(process.argv);
5160
* Convert Bun Lockfile to a Nix expression
5261
*
5362
* @param {string} contents - The contents of a bun lockfile
63+
* @param {Options} options - Lockfile conversion options
5464
* @return {string} The generated nix expression
5565
*/
56-
export function convertLockfileToNixExpression(contents: string): string {
57-
return convert_lockfile_to_nix_expression(contents);
66+
export function convertLockfileToNixExpression(
67+
contents: string,
68+
options: Options,
69+
): string {
70+
return convert_lockfile_to_nix_expression(contents, options);
5871
}

programs/bun2nix/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bun2nix",
3-
"version": "2.0.4",
3+
"version": "2.0.5",
44
"module": "index.ts",
55
"bin": {
66
"bun2nix": "index.ts"

programs/bun2nix/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
pub mod error;
77
pub mod lockfile;
88
pub mod nix_expression;
9+
pub mod options;
910
pub mod package;
1011

11-
use askama::Template;
1212
pub use error::{Error, Result};
1313
pub use lockfile::Lockfile;
1414
use nix_expression::NixExpression;
15+
pub use options::Options;
1516
pub use package::Package;
1617

1718
#[cfg(target_arch = "wasm32")]
@@ -22,7 +23,7 @@ use wasm_bindgen::prelude::*;
2223
/// Takes a string input of the contents of a bun lockfile and converts it into a ready to use Nix expression which fetches the packages
2324
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
2425
#[cfg_attr(target_arch = "wasm32", no_mangle)]
25-
pub fn convert_lockfile_to_nix_expression(contents: String) -> Result<String> {
26+
pub fn convert_lockfile_to_nix_expression(contents: String, options: Options) -> Result<String> {
2627
let lockfile = contents.parse::<Lockfile>()?;
2728

2829
if lockfile.lockfile_version != 1 {
@@ -33,5 +34,5 @@ pub fn convert_lockfile_to_nix_expression(contents: String) -> Result<String> {
3334
packages.sort();
3435
packages.dedup_by(|a, b| a.name == b.name);
3536

36-
Ok(NixExpression::new(packages)?.render()?)
37+
NixExpression::new(packages)?.render_with_options(options)
3738
}

programs/bun2nix/src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
#![warn(missing_docs)]
55

6-
use bun2nix::{Result, convert_lockfile_to_nix_expression};
6+
use bun2nix::{Options, Result, convert_lockfile_to_nix_expression};
77
use log::error;
88

99
use std::{
@@ -27,6 +27,10 @@ pub struct Cli {
2727
/// if no file location is provided, print to stdout instead.
2828
#[arg(short, long)]
2929
output_file: Option<PathBuf>,
30+
31+
/// The prefix to use when copying workspace or file packages
32+
#[arg(short, long, default_value = "./")]
33+
copy_prefix: String,
3034
}
3135

3236
fn main() {
@@ -48,7 +52,12 @@ fn run() -> Result<()> {
4852

4953
let lockfile = fs::read_to_string(&cli.lock_file)?;
5054

51-
let nix = convert_lockfile_to_nix_expression(lockfile)?;
55+
let nix = convert_lockfile_to_nix_expression(
56+
lockfile,
57+
Options {
58+
copy_prefix: cli.copy_prefix,
59+
},
60+
)?;
5261

5362
if let Some(output_file) = cli.output_file {
5463
let mut output = File::create(output_file)?;

programs/bun2nix/src/nix_expression.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ mod nix_escaper;
44

55
pub use nix_escaper::NixEscaper;
66

7-
use crate::error::Result;
7+
use crate::{Options, error::Result};
88
use askama::Template;
9+
use std::{any::Any, collections::HashMap};
910

1011
use crate::Package;
1112

@@ -25,4 +26,14 @@ impl NixExpression {
2526
pub fn new(packages: Vec<Package>) -> Result<Self> {
2627
Ok(Self { packages })
2728
}
29+
30+
/// # Render with options
31+
///
32+
/// Renders a `NixExpression` with the supplied config options
33+
pub fn render_with_options(self, options: Options) -> Result<String> {
34+
let mut values: HashMap<&str, Box<dyn Any>> = HashMap::new();
35+
values.insert("options", Box::new(options));
36+
37+
Ok(self.render_with_values(&values)?)
38+
}
2839
}

0 commit comments

Comments
 (0)