pre-commit magic

This commit is contained in:
Malte Tammena 2024-03-13 11:10:32 +01:00
parent 4d0d1cf9be
commit 5bb40aeade
6 changed files with 237 additions and 416 deletions

View file

@ -21,22 +21,6 @@
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1673956053,
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-parts": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
@ -55,45 +39,6 @@
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1685518550,
"narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"pre-commit-hooks-nix",
"nixpkgs"
]
},
"locked": {
"lastModified": 1660459072,
"narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "a20de23b925fd8264fd7fad6454652e142fd7f73",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1699065553,
@ -128,83 +73,11 @@
"type": "github"
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1685801374,
"narHash": "sha256-otaSUoFEMM+LjBI1XL/xGB5ao6IwnZOXc47qhIgJe8U=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "c37ca420157f4abc31e26f436c1145f8951ff373",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-23.05",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1689261696,
"narHash": "sha256-LzfUtFs9MQRvIoQ3MfgSuipBVMXslMPH/vZ+nM40LkA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "df1eee2aa65052a18121ed4971081576b25d6b5c",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_3": {
"locked": {
"lastModified": 1695644571,
"narHash": "sha256-asS9dCCdlt1lPq0DLwkVBbVoEKuEuz+Zi3DG7pR/RxA=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "6500b4580c2a1f3d0f980d32d285739d8e156d92",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit-hooks-nix": {
"inputs": {
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"gitignore": "gitignore",
"nixpkgs": "nixpkgs_2",
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1698852633,
"narHash": "sha256-Hsc/cCHud8ZXLvmm8pxrXpuaPEeNaaUttaCvtdX/Wug=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "dec10399e5b56aa95fcd530e0338be72ad6462a0",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"fenix": "fenix",
"flake-parts": "flake-parts",
"nixpkgs": "nixpkgs",
"pre-commit-hooks-nix": "pre-commit-hooks-nix",
"treefmt-nix": "treefmt-nix"
"nixpkgs": "nixpkgs"
}
},
"rust-analyzer-src": {
@ -223,39 +96,6 @@
"repo": "rust-analyzer",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"treefmt-nix": {
"inputs": {
"nixpkgs": "nixpkgs_3"
},
"locked": {
"lastModified": 1698438538,
"narHash": "sha256-AWxaKTDL3MtxaVTVU5lYBvSnlspOS0Fjt8GxBgnU0Do=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "5deb8dc125a9f83b65ca86cf0c8167c46593e0b1",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "treefmt-nix",
"type": "github"
}
}
},
"root": "root",

182
flake.nix
View file

@ -1,25 +1,19 @@
{
inputs.flake-parts.url = "github:hercules-ci/flake-parts";
inputs.treefmt-nix.url = "github:numtide/treefmt-nix";
inputs.pre-commit-hooks-nix.url = "github:cachix/pre-commit-hooks.nix";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
inputs.fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
outputs = inputs @ {
self,
flake-parts,
treefmt-nix,
pre-commit-hooks-nix,
fenix,
...
}:
flake-parts.lib.mkFlake {inherit inputs self;} {
outputs =
inputs @ { self
, flake-parts
, fenix
, ...
}:
flake-parts.lib.mkFlake { inherit inputs self; } {
imports = [
treefmt-nix.flakeModule
pre-commit-hooks-nix.flakeModule
];
systems = [
@ -30,107 +24,73 @@
flake.hydraJobs.devShells.x86_64-linux = self.devShells.x86_64-linux;
flake.hydraJobs.checks.x86_64-linux = self.checks.x86_64-linux;
perSystem = {
self',
pkgs,
config,
system,
...
}: let
rustToolchain = with fenix.packages.${system};
combine [
(complete.withComponents [
"cargo"
"clippy"
"rust-src"
"rustc"
"rustfmt"
])
rust-analyzer
];
perSystem =
{ self'
, pkgs
, config
, system
, ...
}:
let
rustToolchain = with fenix.packages.${system};
combine [
(complete.withComponents [
"cargo"
"clippy"
"rust-src"
"rustc"
"rustfmt"
])
rust-analyzer
];
rustPlatform = pkgs.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
rustPlatform = pkgs.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
rename = pkgs.writeShellApplication {
name = "rename";
runtimeInputs = [
pkgs.fd
pkgs.coreutils
pkgs.nixFlakes
pkgs.jq
];
text = ''
newName="$1"
pushd "$(nix flake metadata --json 2>/dev/null | jq .original.path -r)"
# This is only used to prevent the literal name from appearing here, as the rename would find it!
oldName=$(echo 'change_my_name' | sed 's/_/-/g')
stat "nix/packages/$oldName.nix" 2>/dev/null >/dev/null || echo -e "\033[31mCan only be done once\033[0m"
stat "nix/packages/$oldName.nix" 2>/dev/null >/dev/null || exit 1
fd --type f --exec sed "s/aba2sat/$newName/g" -i '{}'
mv nix/packages/aba2sat.nix "nix/packages/$newName.nix"
'';
};
in {
pre-commit.check.enable = false;
pre-commit.settings.hooks.markdownlint.enable = true;
pre-commit.settings.hooks.nil.enable = true;
pre-commit.settings.hooks.format = {
enable = true;
entry = "${self'.formatter}/bin/treefmt";
pass_filenames = false;
};
pre-commit.settings.hooks.my-clippy = {
enable = true;
name = "clippy";
description = "Lint Rust code.";
entry = "${rustToolchain}/bin/cargo clippy --offline -- -D warnings";
files = "\\.rs$";
pass_filenames = false;
};
pre-commit.settings.hooks.my-cargo-check = {
enable = true;
description = "check the cargo package for errors.";
entry = "${rustToolchain}/bin/cargo check --offline";
files = "\\.rs$";
pass_filenames = false;
};
rename = pkgs.writeShellApplication {
name = "rename";
runtimeInputs = [
pkgs.fd
pkgs.coreutils
pkgs.nixFlakes
pkgs.jq
];
text = ''
newName="$1"
pushd "$(nix flake metadata --json 2>/dev/null | jq .original.path -r)"
# This is only used to prevent the literal name from appearing here, as the rename would find it!
oldName=$(echo 'change_my_name' | sed 's/_/-/g')
stat "nix/packages/$oldName.nix" 2>/dev/null >/dev/null || echo -e "\033[31mCan only be done once\033[0m"
stat "nix/packages/$oldName.nix" 2>/dev/null >/dev/null || exit 1
fd --type f --exec sed "s/aba2sat/$newName/g" -i '{}'
mv nix/packages/aba2sat.nix "nix/packages/$newName.nix"
'';
};
in
{
packages.aba2sat = pkgs.callPackage ./nix/packages/aba2sat.nix { inherit rustPlatform; };
packages.aspforaba = pkgs.callPackage ./nix/packages/aspforaba.nix { };
packages.clingo = pkgs.callPackage ./nix/packages/clingo.nix { };
packages.default = self'.packages.aba2sat;
treefmt.projectRootFile = "flake.nix";
treefmt.programs = {
rustfmt.enable = true;
alejandra.enable = true;
devShells.default = pkgs.mkShell {
name = "aba2sat";
nativeBuildInputs = [
pkgs.cargo-workspaces
pkgs.nil
pkgs.pre-commit
pkgs.nodejs
pkgs.shellcheck
pkgs.shfmt
rustToolchain
rename
self'.packages.aspforaba
];
RUST_LOG = "trace";
};
devShells.pre-commit = config.pre-commit.devShell;
};
treefmt.flakeFormatter = true;
packages.aba2sat = pkgs.callPackage ./nix/packages/aba2sat.nix {inherit rustPlatform;};
packages.aspforaba = pkgs.callPackage ./nix/packages/aspforaba.nix {};
packages.clingo = pkgs.callPackage ./nix/packages/clingo.nix {};
packages.default = self'.packages.aba2sat;
devShells.default = pkgs.mkShell {
name = "aba2sat";
shellHook = ''
${config.pre-commit.installationScript}
# This is only used to prevent the literal name from appearing here, as the rename would find it!
oldName=$(echo 'change_my_name' | sed 's/_/-/g')
echo -e 1>&2 "\n\n Welcome to the development shell!"
stat "nix/packages/$oldName.nix" 2>/dev/null >/dev/null && echo -e 1>&2 "\n \033[31mChange this projects name with \033[1mrename NEW-NAME\033[0m"
echo -e 1>&2 "\n"
'';
nativeBuildInputs = [
config.treefmt.package
pkgs.cargo-workspaces
pkgs.nil
rustToolchain
rename
self'.packages.aspforaba
];
RUST_LOG = "trace";
};
devShells.pre-commit = config.pre-commit.devShell;
};
};
}

View file

@ -1,27 +1,28 @@
{
rustPlatform,
lib,
}: let
{ rustPlatform
, lib
,
}:
let
config = lib.trivial.importTOML ../../Cargo.toml;
in
rustPlatform.buildRustPackage {
pname = config.package.name;
version = config.package.version;
rustPlatform.buildRustPackage {
pname = config.package.name;
version = config.package.version;
src = lib.sources.cleanSource ../..;
src = lib.sources.cleanSource ../..;
cargoDeps = {
lockFile = "../../Cargo.lock";
};
cargoDeps = {
lockFile = "../../Cargo.lock";
};
cargoLock = {
lockFile = ../../Cargo.lock;
};
cargoLock = {
lockFile = ../../Cargo.lock;
};
meta = {
description = "ABA solver using a SAT backend";
# homepage = "https://your.new.homepage";
license = lib.licenses.gpl3;
# maintainers = [];
};
}
meta = {
description = "ABA solver using a SAT backend";
# homepage = "https://your.new.homepage";
license = lib.licenses.gpl3;
# maintainers = [];
};
}

View file

@ -1,8 +1,9 @@
{
fetchFromBitbucket,
python3,
clingo,
}: let
{ fetchFromBitbucket
, python3
, clingo
,
}:
let
name = "ASPforABA";
version = "ICCMA23";
src = fetchFromBitbucket {
@ -12,32 +13,32 @@
hash = "sha256-QdcisBOsGPOq9/KCQAUpKzQS7E2Olg4Zv+0jmf3/GkU=";
};
clingoWithPython = clingo.overrideAttrs (old: {
cmakeFlags = ["-DCLINGO_BUILD_WITH_PYTHON=ON"];
nativeBuildInputs = old.nativeBuildInputs ++ [python3];
cmakeFlags = [ "-DCLINGO_BUILD_WITH_PYTHON=ON" ];
nativeBuildInputs = old.nativeBuildInputs ++ [ python3 ];
});
in
python3.pkgs.buildPythonPackage {
inherit src version;
pname = name;
python3.pkgs.buildPythonPackage {
inherit src version;
pname = name;
format = "other";
format = "other";
pythonPath = [clingoWithPython python3.pkgs.cffi];
pythonPath = [ clingoWithPython python3.pkgs.cffi ];
makeWrapperArgs = ["--run 'mkdir -p /tmp/clingo'"];
makeWrapperArgs = [ "--run 'mkdir -p /tmp/clingo'" ];
patchPhase = ''
rm ./configure
'';
patchPhase = ''
rm ./configure
'';
installPhase = ''
mkdir -p $out/bin
cp aspforaba.py $out/bin/ASPforABA
cp -r encodings $out/bin/
installPhase = ''
mkdir -p $out/bin
cp aspforaba.py $out/bin/ASPforABA
cp -r encodings $out/bin/
cat << EOF > $out/bin/.config
TEMP_PATH=/tmp/clingo
CLINGO_PATH=${clingoWithPython}/bin/clingo
EOF
'';
}
cat << EOF > $out/bin/.config
TEMP_PATH=/tmp/clingo
CLINGO_PATH=${clingoWithPython}/bin/clingo
EOF
'';
}

View file

@ -1,9 +1,9 @@
{
lib,
stdenv,
fetchFromGitHub,
cmake,
python3,
{ lib
, stdenv
, fetchFromGitHub
, cmake
, python3
,
}:
stdenv.mkDerivation rec {
pname = "clingo";
@ -16,14 +16,14 @@ stdenv.mkDerivation rec {
sha256 = "sha256-2vOscD5jengY3z9gHoY9y9y6RLfdzUj7BNKLyppNRac=";
};
nativeBuildInputs = [cmake python3];
nativeBuildInputs = [ cmake python3 ];
cmakeFlags = ["-DCLINGO_BUILD_WITH_PYTHON=ON"];
cmakeFlags = [ "-DCLINGO_BUILD_WITH_PYTHON=ON" ];
meta = {
description = "ASP system to ground and solve logic programs";
license = lib.licenses.mit;
maintainers = [lib.maintainers.raskin];
maintainers = [ lib.maintainers.raskin ];
platforms = lib.platforms.unix;
homepage = "https://potassco.org/";
downloadPage = "https://github.com/potassco/clingo/releases/";

View file

@ -1,44 +1,46 @@
#!/usr/bin/env bash
print_help_and_exit() {
if [ -n "$1" ]; then
printf "$1\n\n"
fi
printf "Usage: validate [OPTIONS] \n"
printf "\n"
printf "Options:\n"
printf " --aspforaba\n"
printf " Binary to use when calling aspforaba\n"
printf " -p, --problem\n"
printf " The problem to solve\n"
printf " -a, --arg\n"
printf " The additional argument for the problem\n"
printf " -f, --file\n"
printf " The file containing the problem in ABA format\n"
printf " --files-from\n"
printf " Use the following dir to read files, specify a single file with --file instead\n"
exit 1
if [ -n "$1" ]; then
printf "%s\n\n" "$1"
fi
printf "Usage: validate [OPTIONS] \n"
printf "\n"
printf "Options:\n"
printf " --aspforaba\n"
printf " Binary to use when calling aspforaba\n"
printf " -p, --problem\n"
printf " The problem to solve\n"
printf " -a, --arg\n"
printf " The additional argument for the problem\n"
printf " -f, --file\n"
printf " The file containing the problem in ABA format\n"
printf " --files-from\n"
printf " Use the following dir to read files, specify a single file with --file instead\n"
exit 1
}
run_dc_co() {
if [ ! -n "$ADDITIONAL_ARG" ]; then
print_help_and_exit "Parameter --arg is missing!"
fi
if [ ! -n "$ABA_FILE" ]; then
print_help_and_exit "Parameter --file is missing!"
fi
printf "%40s " $(basename $ABA_FILE)
our_result=$($ABA2SAT --file "$ABA_FILE" dc-co --query "$ADDITIONAL_ARG")
other_result=$($ASPFORABA --file "$ABA_FILE" --problem DC-CO --query "$ADDITIONAL_ARG")
if [ "$our_result" != "$other_result" ]; then
printf "❌\n"
printf "%40s:%s\n" "arg" "$ADDITIONAL_ARG"
printf "%40s:%s\n" "Ours" "$our_result"
printf "%40s:%s\n" "Theirs" "$other_result"
else
printf "✅\n"
fi
if [ -z "$ADDITIONAL_ARG" ]; then
print_help_and_exit "Parameter --arg is missing!"
fi
if [ -z "$ABA_FILE" ]; then
print_help_and_exit "Parameter --file is missing!"
fi
printf "%40s " "$(basename "$ABA_FILE")"
TIMEFORMAT='{"wall":"%E","system":"%S","user":"%U"}'
our_result=$(command time -f "$TIMEFORMAT" -o /tmp/aba2sat-time "$ABA2SAT" --file "$ABA_FILE" dc-co --query "$ADDITIONAL_ARG")
other_result=$(command time -f "$TIMEFORMAT" -o /tmp/aspforaba-time "$ASPFORABA" --file "$ABA_FILE" --problem DC-CO --query "$ADDITIONAL_ARG")
if [ "$our_result" != "$other_result" ]; then
printf "❌\n"
printf "%40s:%s\n" "arg" "$ADDITIONAL_ARG"
printf "%40s:%40s %s\n" "Ours" "$our_result" "$(jq /tmp/aba2sat-time --compact-output --color-output)"
printf "%40s:%40s %s\n" "Theirs" "$other_result" "$(jq /tmp/aspforaba-time --compact-output --color-output)"
else
printf "✅\n"
printf "%40s:%40s %s\n" "Ours" "$our_result" "$(jq /tmp/aba2sat-time --compact-output --color-output)"
printf "%40s:%40s %s\n" "Theirs" "$other_result" "$(jq /tmp/aspforaba-time --compact-output --color-output)"
fi
}
POSITIONAL_ARGS=()
@ -47,65 +49,82 @@ ABA2SAT=result/bin/aba2sat
ABA_FILE=
ABA_FILE_DIR=
ABA_FILE_EXT=aba
ADDITIONAL_ARG_FILE_EXT=asm
PROBLEM=
ADDITIONAL_ARG=
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
print_help_and_exit;;
--aspforaba)
shift; ASPFORABA=$1; shift;;
-p | --problem)
shift; PROBLEM=$1; shift;;
-f | --file)
if [ -n "$ABA_FILE_DIR" ]; then
print_help_and_exit "Parameters --file and --files-from cannot be mixed"
fi
shift; ABA_FILE=$1; shift;;
--files-from)
if [ -n "$ABA_FILE" ]; then
print_help_and_exit "Parameters --file and --files-from cannot be mixed"
fi
if [ -n "$ADDITIONAL_ARG" ]; then
print_help_and_exit "Parameters --arg and --files-from cannot be mixed"
fi
shift; ABA_FILE_DIR=$1; shift;;
-a | --arg)
if [ -n "$ABA_FILE_DIR" ]; then
print_help_and_exit "Parameters --arg and --files-from cannot be mixed"
fi
shift; ADDITIONAL_ARG=$1; shift;;
--aba2sat)
shift; ABA2SAT=$1; shift;;
-*)
echo "Unknown option $1"
print_help_and_exit
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
case $1 in
-h | --help)
print_help_and_exit
;;
--aspforaba)
shift
ASPFORABA=$1
shift
;;
-p | --problem)
shift
PROBLEM=$1
shift
;;
-f | --file)
if [ -n "$ABA_FILE_DIR" ]; then
print_help_and_exit "Parameters --file and --files-from cannot be mixed"
fi
shift
ABA_FILE=$1
shift
;;
--files-from)
if [ -n "$ABA_FILE" ]; then
print_help_and_exit "Parameters --file and --files-from cannot be mixed"
fi
if [ -n "$ADDITIONAL_ARG" ]; then
print_help_and_exit "Parameters --arg and --files-from cannot be mixed"
fi
shift
ABA_FILE_DIR=$1
shift
;;
-a | --arg)
if [ -n "$ABA_FILE_DIR" ]; then
print_help_and_exit "Parameters --arg and --files-from cannot be mixed"
fi
shift
ADDITIONAL_ARG=$1
shift
;;
--aba2sat)
shift
ABA2SAT=$1
shift
;;
-*)
echo "Unknown option $1"
print_help_and_exit
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "''${POSITIONAL_ARGS[@]}" # restore positional parameters
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
case "$PROBLEM" in
dc-co | DC-CO)
if [ -n "$ABA_FILE_DIR" ]; then
# run for every file found in the directory
for file in "$ABA_FILE_DIR"/*."$ABA_FILE_EXT"; do
ABA_FILE="$file" ADDITIONAL_ARG="$(cat $file.asm)" run_dc_co
done
else
# run for the single configured file
run_dc_co
fi
;;
*)
print_help_and_exit;;
dc-co | DC-CO)
if [ -n "$ABA_FILE_DIR" ]; then
# run for every file found in the directory
for file in "$ABA_FILE_DIR"/*."$ABA_FILE_EXT"; do
ABA_FILE="$file" ADDITIONAL_ARG="$(cat "$file.asm")" run_dc_co
done
else
# run for the single configured file
run_dc_co
fi
;;
*)
print_help_and_exit "Problem $PROBLEM is not supported"
;;
esac