diff --git a/.envrc b/.envrc index 3550a30..fd3f77f 100644 --- a/.envrc +++ b/.envrc @@ -1 +1,3 @@ +watch_dir nix + use flake diff --git a/.gitignore b/.gitignore index b609f70..9631fad 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .flatpak-builder/ .ignore/ .direnv/ +/.pre-commit-config.yaml # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/flake.lock b/flake.lock index bffd844..35b8c66 100644 --- a/flake.lock +++ b/flake.lock @@ -1,6 +1,79 @@ { "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1767039857, + "narHash": "sha256-vNpUSpF5Nuw8xvDLj2KCwwksIbjua2LZCqhV1LNRDns=", + "owner": "NixOS", + "repo": "flake-compat", + "rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "flake-compat", + "type": "github" + } + }, + "git-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "gitignore": "gitignore", + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1778507602, + "narHash": "sha256-kTwur1wV+01SdqskVMSo6JMEpg71ps3HpbFY2GsflKs=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "61ab0e80d9c7ab14c256b5b453d8b3fb0189ba0a", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "git-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, "nixpkgs": { + "locked": { + "lastModified": 1770073757, + "narHash": "sha256-Vy+G+F+3E/Tl+GMNgiHl9Pah2DgShmIUBJXmbiQPHbI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "47472570b1e607482890801aeaf29bfb749884f6", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { "locked": { "lastModified": 1780243769, "narHash": "sha256-x5UQuRsH3MqI0U9afaXSNqzTPSeZlRLvFAav2Ux1pNw=", @@ -18,7 +91,8 @@ }, "root": { "inputs": { - "nixpkgs": "nixpkgs" + "git-hooks": "git-hooks", + "nixpkgs": "nixpkgs_2" } } }, diff --git a/flake.nix b/flake.nix index 50c7279..8384b6a 100644 --- a/flake.nix +++ b/flake.nix @@ -3,6 +3,7 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + git-hooks.url = "github:cachix/git-hooks.nix"; }; outputs = @@ -12,6 +13,7 @@ ... }: let + inherit (self) inputs; systems = [ "x86_64-linux" "aarch64-linux" @@ -28,7 +30,7 @@ in { default = pkgs.callPackage ./nix/package.nix { }; - anifetch = self.packages.default; + anifetch = self.packages.${system}.default; } ); @@ -39,17 +41,97 @@ anifetch = self.overlays.default; }; - formatter = forAllSystems (system: nixpkgs.legacyPackages.${system}.nixfmt-tree); + formatter = forAllSystems ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + config = self.checks.${system}.pre-commit-check.config; + inherit (config) package configFile; + script = '' + ${pkgs.lib.getExe package} run --all-files --config ${configFile} + ''; + in + pkgs.writeShellScriptBin "pre-commit-run" script + ); + + checks = forAllSystems ( + system: + let + pkgs = import nixpkgs { inherit system; }; + in + { + pre-commit-check = inputs.git-hooks.lib.${system}.run { + src = ./.; + hooks = { + nixfmt.enable = true; + + ruff-check = { + enable = true; + entry = "${pkgs.lib.getExe pkgs.ruff}"; + args = [ + "check" + "--fix" + ]; + types = [ + "file" + "python" + ]; + }; + ruff-format = { + enable = true; + entry = "${pkgs.lib.getExe pkgs.ruff}"; + args = [ + "format" + ]; + types = [ + "file" + "python" + ]; + }; + }; + + package = pkgs.prek; + }; + } + ); - devShell = forAllSystems ( + devShells = forAllSystems ( system: let pkgs = import nixpkgs { inherit system; }; + myPython = pkgs.python3; + pythonWithPkgs = myPython.withPackages (ps: [ + ps.pip + ps.setuptools + ]); + + venv = "venv"; + inherit (self.checks.${system}.pre-commit-check) shellHook enabledPackages; in - pkgs.mkShell { - packages = [ - self.packages.${pkgs.stdenv.hostPlatform.system}.default - ]; + { + default = pkgs.mkShell { + nativeBuildInputs = [ + pythonWithPkgs + pkgs.bc + pkgs.chafa + pkgs.ffmpeg + ] + ++ enabledPackages; + shellHook = shellHook + '' + export "CPATH=${pkgs.linuxHeaders}/include:$CPATH" + if [ ! -d "${venv}" ]; then + echo "Creating Python venv..." + python3 -m venv ${venv} + fi + echo "Activating venv..." + source ${venv}/bin/activate + if ! pip show anifetch &>/dev/null; then + echo "Aniftech not installed: Install anifetch..." + pip install -e . + fi + echo "Venv activated." + ''; + }; } ); }; diff --git a/nix/package.nix b/nix/package.nix index 7fab868..8a74592 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -8,10 +8,11 @@ let fs = lib.fileset; sourceFiles = ../.; + anifetchPyprojectToml = fromTOML (builtins.readFile ../pyproject.toml); in fs.trace sourceFiles python3Packages.buildPythonApplication { - name = "anifetch-wrapped"; - version = "git"; + pname = "anifetch"; + version = "${anifetchPyprojectToml.project.version}-git"; pyproject = true; src = fs.toSource { root = ../.; diff --git a/ruff.sh b/ruff.sh index 2612939..fac5e86 100755 --- a/ruff.sh +++ b/ruff.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash ruff check . --fix -ruff format . \ No newline at end of file +ruff format .