Skip to content

Commit

Permalink
add nixfmt, use as default over nixpkgs-fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-martin authored and pbrisbin committed Jul 11, 2024
1 parent a69482f commit 3ede8d7
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 1 deletion.
8 changes: 8 additions & 0 deletions nixfmt/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM nixos/nix:2.23.1
LABEL maintainer="Chris Martin <[email protected]>"
RUN echo "experimental-features = nix-command flakes" > /etc/nix/nix.conf
RUN nix profile install --impure 'github:NixOS/nixfmt?rev=698954723ecec3f91770460ecae762ce590f2d9e'
RUN nix profile install --impure 'nixpkgs#gnused'
RUN mkdir -p /code
WORKDIR /code
CMD ["nixfmt", "--help"]
194 changes: 194 additions & 0 deletions nixfmt/info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
enabled: true
name: nixfmt
version_cmd: |
nixfmt --version | sed 's/^nixfmt \(.*\)$/\1/'
command:
- nixfmt
supports_arg_sep: true
supports_multiple_files: true
include:
- "**/*.nix"
documentation:
- https:/NixOS/nixfmt#readme
metadata:
languages:
- Nix
tests:
- name: Online demo
contents: |
let
/*
* What you're seeing here is our nix formatter. It's quite opinionated:
*/
sample-01 = { lib }:
{
list = [
elem1
elem2
elem3
] ++ lib.optionals stdenv.isDarwin [
elem4
elem5
]; # and not quite finished
}; # it will preserve your newlines
sample-02 = { stdenv, lib }:
{
list =
[
elem1
elem2
elem3
]
++ lib.optionals stdenv.isDarwin [ elem4 elem5 ]
++ lib.optionals stdenv.isLinux [ elem6 ]
;
};
# but it can handle all nix syntax,
# and, in fact, all of nixpkgs in <20s.
# The javascript build is quite a bit slower.
sample-03 = { stdenv, system }:
assert system == "i686-linux";
stdenv.mkDerivation { };
# these samples are all from https:/nix-community/nix-fmt/tree/master/samples
sample-simple = # Some basic formatting
{
empty_list = [ ];
inline_list = [ 1 2 3 ];
multiline_list = [
1
2
3
4
];
inline_attrset = { x = "y"; };
multiline_attrset = {
a = 3;
b = 5;
};
# some comment over here
fn = x: x + x;
relpath = ./hello;
abspath = /hello;
# URLs get converted from strings
url = "https://foobar.com";
atoms = [ true false null ];
# Combined
listOfAttrs = [
{
attr1 = 3;
attr2 = "fff";
}
{
attr1 = 5;
attr2 = "ggg";
}
];
# long expression
attrs = {
attr1 = short_expr;
attr2 =
if true then big_expr else big_expr;
};
}
;
in
[ sample-01 sample-02 sample-03 ]
restyled: |
let
# * What you're seeing here is our nix formatter. It's quite opinionated:
sample-01 =
{ lib }:
{
list =
[
elem1
elem2
elem3
]
++ lib.optionals stdenv.isDarwin [
elem4
elem5
]; # and not quite finished
}; # it will preserve your newlines
sample-02 =
{ stdenv, lib }:
{
list =
[
elem1
elem2
elem3
]
++ lib.optionals stdenv.isDarwin [
elem4
elem5
]
++ lib.optionals stdenv.isLinux [ elem6 ];
};
# but it can handle all nix syntax,
# and, in fact, all of nixpkgs in <20s.
# The javascript build is quite a bit slower.
sample-03 =
{ stdenv, system }:
assert system == "i686-linux";
stdenv.mkDerivation { };
# these samples are all from https:/nix-community/nix-fmt/tree/master/samples
sample-simple = # Some basic formatting
{
empty_list = [ ];
inline_list = [
1
2
3
];
multiline_list = [
1
2
3
4
];
inline_attrset = {
x = "y";
};
multiline_attrset = {
a = 3;
b = 5;
};
# some comment over here
fn = x: x + x;
relpath = ./hello;
abspath = /hello;
# URLs get converted from strings
url = "https://foobar.com";
atoms = [
true
false
null
];
# Combined
listOfAttrs = [
{
attr1 = 3;
attr2 = "fff";
}
{
attr1 = 5;
attr2 = "ggg";
}
];
# long expression
attrs = {
attr1 = short_expr;
attr2 = if true then big_expr else big_expr;
};
};
in
[
sample-01
sample-02
sample-03
]
2 changes: 1 addition & 1 deletion nixpkgs-fmt/info.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enabled: true
enabled: false
name: nixpkgs-fmt
version_cmd: |
nixpkgs-fmt --version | sed 's/^nixpkgs-fmt \(.*\)$/v\1/'
Expand Down

0 comments on commit 3ede8d7

Please sign in to comment.