Skip to content

Commit

Permalink
Format Rust, Nix and Bash via treefmt (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
philiptaron committed Apr 18, 2024
2 parents 62d820f + ff95d9b commit 6090c42
Show file tree
Hide file tree
Showing 49 changed files with 226 additions and 220 deletions.
12 changes: 12 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file contains a list of commits that are not likely what you
# are looking for in a blame, such as mass reformatting or renaming.
# You can set this file as a default ignore file for blame by running
# the following command.
#
# $ git config blame.ignoreRevsFile .git-blame-ignore-revs

# Reformatted all Nix files
81d712a6c4e020bf545fb931b3106aff64871962

# Reformatted all Bash scripts
b1306dd5ae96b0fb934256d91708733c1f850016
70 changes: 48 additions & 22 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ in
{
system ? builtins.currentSystem,
nixpkgs ? sources.nixpkgs,
treefmt-nix ? sources.treefmt-nix,
}:
let
pkgs = import nixpkgs {
inherit system;
config = {};
overlays = [];
config = { };
overlays = [ ];
};
inherit (pkgs) lib;

Expand All @@ -34,12 +35,28 @@ let
# Determine version from Cargo.toml
version = (lib.importTOML ./Cargo.toml).package.version;

treefmtEval = (import treefmt-nix).evalModule pkgs {
# Used to find the project root
projectRootFile = ".git/config";

programs.rustfmt.enable = true;
programs.nixfmt-rfc-style.enable = true;
programs.shfmt.enable = true;
settings.formatter.shfmt.options = [ "--space-redirects" ];
};

results = {
# We're using this value as the root result. By default, derivations expose all of their
# internal attributes, which is very messy. We prevent this using lib.lazyDerivation
build = lib.lazyDerivation {
derivation = pkgs.callPackage ./package.nix {
inherit nixpkgsLibPath initNix runtimeExprPath testNixpkgsPath version;
inherit
nixpkgsLibPath
initNix
runtimeExprPath
testNixpkgsPath
version
;
};
};

Expand All @@ -51,18 +68,25 @@ let
nativeBuildInputs = with pkgs; [
npins
rust-analyzer
treefmtEval.config.build.wrapper
];
};

# This checks that all Git-tracked files are formatted appropriately
treefmt = treefmtEval.config.build.check (
lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.gitTracked ./.;
}
);

# Run regularly by CI and turned into a PR
autoPrUpdate =
let
updateScripts = {
npins = pkgs.writeShellApplication {
name = "update-npins";
runtimeInputs = with pkgs; [
npins
];
runtimeInputs = with pkgs; [ npins ];
text = ''
echo "<details><summary>npins changes</summary>"
# Needed because GitHub's rendering of the first body line breaks down otherwise
Expand All @@ -75,9 +99,7 @@ let
};
cargo = pkgs.writeShellApplication {
name = "update-cargo";
runtimeInputs = with pkgs; [
cargo
];
runtimeInputs = with pkgs; [ cargo ];
text = ''
echo "<details><summary>cargo changes</summary>"
# Needed because GitHub's rendering of the first body line breaks down otherwise
Expand Down Expand Up @@ -113,21 +135,25 @@ let
};

# Tests the tool on the pinned Nixpkgs tree, this is a good sanity check
nixpkgsCheck = pkgs.runCommand "test-nixpkgs-check-by-name" {
nativeBuildInputs = [
results.build
pkgs.nix
];
nixpkgsPath = nixpkgs;
} ''
${initNix}
nixpkgs-check-by-name --base "$nixpkgsPath" "$nixpkgsPath"
touch $out
'';
nixpkgsCheck =
pkgs.runCommand "test-nixpkgs-check-by-name"
{
nativeBuildInputs = [
results.build
pkgs.nix
];
nixpkgsPath = nixpkgs;
}
''
${initNix}
nixpkgs-check-by-name --base "$nixpkgsPath" "$nixpkgsPath"
touch $out
'';
};

in
results.build // results // {
results.build
// results
// {

# Good for debugging
inherit pkgs;
Expand Down
48 changes: 33 additions & 15 deletions npins/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@ let
data = builtins.fromJSON (builtins.readFile ./sources.json);
version = data.version;

mkSource = spec:
assert spec ? type; let
mkSource =
spec:
assert spec ? type;
let
path =
if spec.type == "Git" then mkGitSource spec
else if spec.type == "GitRelease" then mkGitSource spec
else if spec.type == "PyPi" then mkPyPiSource spec
else if spec.type == "Channel" then mkChannelSource spec
else builtins.throw "Unknown source type ${spec.type}";
if spec.type == "Git" then
mkGitSource spec
else if spec.type == "GitRelease" then
mkGitSource spec
else if spec.type == "PyPi" then
mkPyPiSource spec
else if spec.type == "Channel" then
mkChannelSource spec
else
builtins.throw "Unknown source type ${spec.type}";
in
spec // { outPath = path; };

mkGitSource = { repository, revision, url ? null, hash, ... }:
mkGitSource =
{
repository,
revision,
url ? null,
hash,
...
}:
assert repository ? type;
# At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
# In the latter case, there we will always be an url to the tarball
Expand All @@ -23,19 +37,23 @@ let
inherit url;
sha256 = hash; # FIXME: check nix version & use SRI hashes
})
else assert repository.type == "Git"; builtins.fetchGit {
url = repository.url;
rev = revision;
# hash = hash;
};
else
assert repository.type == "Git";
builtins.fetchGit {
url = repository.url;
rev = revision;
# hash = hash;
};

mkPyPiSource = { url, hash, ... }:
mkPyPiSource =
{ url, hash, ... }:
builtins.fetchurl {
inherit url;
sha256 = hash;
};

mkChannelSource = { url, hash, ... }:
mkChannelSource =
{ url, hash, ... }:
builtins.fetchTarball {
inherit url;
sha256 = hash;
Expand Down
12 changes: 12 additions & 0 deletions npins/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
"name": "nixpkgs-unstable",
"url": "https://releases.nixos.org/nixpkgs/nixpkgs-24.05pre612007.2748d22b45a9/nixexprs.tar.xz",
"hash": "0l6yzjxvlilidsixqz54110n84yzcjsm74wvrs69pvs73gk1w1xa"
},
"treefmt-nix": {
"type": "Git",
"repository": {
"type": "GitHub",
"owner": "numtide",
"repo": "treefmt-nix"
},
"branch": "main",
"revision": "49dc4a92b02b8e68798abd99184f228243b6e3ac",
"url": "https://github.com/numtide/treefmt-nix/archive/49dc4a92b02b8e68798abd99184f228243b6e3ac.tar.gz",
"hash": "0qlhb0xvcc3al19irclxk7vnppd9m6b5vi3nbjb9dylphs306x1p"
}
},
"version": 3
Expand Down
4 changes: 0 additions & 4 deletions package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
lib,
rustPlatform,
nix,
rustfmt,
clippy,
makeWrapper,

Expand Down Expand Up @@ -30,21 +29,18 @@ rustPlatform.buildRustPackage {
cargoLock.lockFile = ./Cargo.lock;
nativeBuildInputs = [
nix
rustfmt
clippy
makeWrapper
];
env.NIX_CHECK_BY_NAME_EXPR_PATH = "${runtimeExprPath}";
env.NIX_PATH = "test-nixpkgs=${testNixpkgsPath}:test-nixpkgs/lib=${nixpkgsLibPath}";
preCheck = initNix;
postCheck = ''
cargo fmt --check
# --tests or --all-targets include tests for linting
cargo clippy --all-targets -- -D warnings
'';
postInstall = ''
wrapProgram $out/bin/nixpkgs-check-by-name \
--set NIX_CHECK_BY_NAME_EXPR_PATH "$NIX_CHECK_BY_NAME_EXPR_PATH"
'';

}
2 changes: 1 addition & 1 deletion scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fi

echo "Building release artifact for system $system"

nix-build "$root" -A build -o "$tmp/build" >/dev/null
nix-build "$root" -A build -o "$tmp/build" > /dev/null
readarray -t closure < <(nix-store -qR "$tmp/build")
nix-store --export "${closure[@]}" > "$tmp/$system.nar"
gzip "$tmp/$system.nar"
Expand Down
10 changes: 5 additions & 5 deletions scripts/update-github-actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trap 'rm -rf "$tmp"' exit

# Use dependency groups to update all dependencies together
# Note that repo is not used because we pass `--local` on the CLI
cat <<EOF > "$tmp/input.yml"
cat << EOF > "$tmp/input.yml"
job:
package-manager: "github_actions"
allowed-updates:
Expand All @@ -39,13 +39,13 @@ job:
EOF

if create_pull_request=$(LOCAL_GITHUB_ACCESS_TOKEN="$githubToken" \
dependabot update --file "$tmp/input.yml" --local "$REPO_ROOT" \
| jq --exit-status 'select(.type == "create_pull_request").data'); then
dependabot update --file "$tmp/input.yml" --local "$REPO_ROOT" |
jq --exit-status 'select(.type == "create_pull_request").data'); then

jq --exit-status --raw-output '."pr-body"' <<< "$create_pull_request"

jq --compact-output '."updated-dependency-files"[]' <<< "$create_pull_request" \
| while read -r fileUpdate; do
jq --compact-output '."updated-dependency-files"[]' <<< "$create_pull_request" |
while read -r fileUpdate; do
file=$(jq --exit-status --raw-output '.name' <<< "$fileUpdate")
# --join-output makes sure to not output a trailing newline
jq --exit-status --raw-output --join-output '.content' <<< "$fileUpdate" > "$REPO_ROOT/$file"
Expand Down
Loading

0 comments on commit 6090c42

Please sign in to comment.