From dbd36b0d23d9519fcb41fc298a602ed1f6e185c4 Mon Sep 17 00:00:00 2001 From: XYenon Date: Sun, 22 Oct 2023 08:10:53 +0800 Subject: [PATCH] refactor(nix): add shell completion and avoid impure (#293) --- .envrc | 1 + .gitignore | 2 +- flake.lock | 229 +++++++------------------------------------------- flake.nix | 24 ++++-- nix/shell.nix | 22 ++--- nix/yazi.nix | 7 +- 6 files changed, 65 insertions(+), 220 deletions(-) create mode 100644 .envrc diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..8392d159 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake \ No newline at end of file diff --git a/.gitignore b/.gitignore index 9df5bdcf..4a903a33 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ config/completions result result-* -.devenv* +.direnv diff --git a/flake.lock b/flake.lock index 7520c92a..4479e15d 100644 --- a/flake.lock +++ b/flake.lock @@ -1,66 +1,9 @@ { "nodes": { - "devenv": { - "inputs": { - "flake-compat": "flake-compat", - "nix": "nix", - "nixpkgs": [ - "nixpkgs" - ], - "pre-commit-hooks": "pre-commit-hooks" - }, - "locked": { - "lastModified": 1695365659, - "narHash": "sha256-VsbuaBJ7Hx3aqdPPRT2Gp+TmAvtJtcffQGChws9VHZM=", - "owner": "cachix", - "repo": "devenv", - "rev": "1f21193496c515e55c556176e6aa13db9fdad1b4", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "devenv", - "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-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" - } - }, - "flake-utils_2": { - "inputs": { - "systems": "systems_2" - }, "locked": { "lastModified": 1694529238, "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", @@ -75,75 +18,13 @@ "type": "github" } }, - "gitignore": { - "inputs": { - "nixpkgs": [ - "devenv", - "pre-commit-hooks", - "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" - } - }, - "lowdown-src": { - "flake": false, - "locked": { - "lastModified": 1633514407, - "narHash": "sha256-Dw32tiMjdK9t3ETl5fzGrutQTzh2rufgZV4A/BbxuD4=", - "owner": "kristapsdz", - "repo": "lowdown", - "rev": "d2c2b44ff6c27b936ec27358a2653caaef8f73b8", - "type": "github" - }, - "original": { - "owner": "kristapsdz", - "repo": "lowdown", - "type": "github" - } - }, - "nix": { - "inputs": { - "lowdown-src": "lowdown-src", - "nixpkgs": [ - "devenv", - "nixpkgs" - ], - "nixpkgs-regression": "nixpkgs-regression" - }, - "locked": { - "lastModified": 1676545802, - "narHash": "sha256-EK4rZ+Hd5hsvXnzSzk2ikhStJnD63odF7SzsQ8CuSPU=", - "owner": "domenkozar", - "repo": "nix", - "rev": "7c91803598ffbcfe4a55c44ac6d49b2cf07a527f", - "type": "github" - }, - "original": { - "owner": "domenkozar", - "ref": "relaxed-flakes", - "repo": "nix", - "type": "github" - } - }, "nixpkgs": { "locked": { - "lastModified": 1695132891, - "narHash": "sha256-cJR9AFHmt816cW/C9necLJyOg/gsnkvEeFAfxgeM1hc=", + "lastModified": 1697793076, + "narHash": "sha256-02e7sCuqLtkyRgrZmdOyvAcQTQdcXj+vpyp9bca6cY4=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "8b5ab8341e33322e5b66fb46ce23d724050f6606", + "rev": "038b2922be3fc096e1d456f93f7d0f4090628729", "type": "github" }, "original": { @@ -153,71 +34,34 @@ "type": "github" } }, - "nixpkgs-regression": { - "locked": { - "lastModified": 1643052045, - "narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", - "type": "github" - }, - "original": { - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", - "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" - } - }, - "pre-commit-hooks": { - "inputs": { - "flake-compat": [ - "devenv", - "flake-compat" - ], - "flake-utils": "flake-utils", - "gitignore": "gitignore", - "nixpkgs": [ - "devenv", - "nixpkgs" - ], - "nixpkgs-stable": "nixpkgs-stable" - }, - "locked": { - "lastModified": 1688056373, - "narHash": "sha256-2+SDlNRTKsgo3LBRiMUcoEUb6sDViRNQhzJquZ4koOI=", - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "rev": "5843cf069272d92b60c3ed9e55b7a8989c01d4c7", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "type": "github" - } - }, "root": { "inputs": { - "devenv": "devenv", - "flake-utils": "flake-utils_2", - "nixpkgs": "nixpkgs" + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1697854201, + "narHash": "sha256-H+0Wb20PQx/8N7X/OfwwAVPeN9TbfjcyG0sXbdgsh50=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "6e8e3332433847cd56186b1f6fc8c47603cf5b46", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" } }, "systems": { @@ -234,21 +78,6 @@ "repo": "default", "type": "github" } - }, - "systems_2": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 7e8f005a..a5af3907 100644 --- a/flake.nix +++ b/flake.nix @@ -2,16 +2,30 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; flake-utils.url = "github:numtide/flake-utils"; - devenv = { - url = "github:cachix/devenv"; - inputs.nixpkgs.follows = "nixpkgs"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs = { + nixpkgs.follows = "nixpkgs"; + flake-utils.follows = "flake-utils"; + }; }; }; - outputs = { self, nixpkgs, flake-utils, ... } @ inputs: + outputs = { self, nixpkgs, flake-utils, rust-overlay, ... } @ inputs: + let + # Nixpkgs overlays + overlays = [ + rust-overlay.overlays.default + (final: prev: { + rustToolchain = final.rust-bin.stable.latest.default.override { + extensions = [ "rust-src" ]; + }; + }) + ]; + in flake-utils.lib.eachDefaultSystem (system: let - pkgs = nixpkgs.legacyPackages.${system}; + pkgs = import nixpkgs { inherit system overlays; }; versionSuffix = "pre${builtins.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}_${self.shortRev or "dirty"}"; version = (builtins.fromTOML (builtins.readFile ./app/Cargo.toml)).package.version + versionSuffix; yazi = pkgs.callPackage ./nix/yazi.nix { inherit version; }; diff --git a/nix/shell.nix b/nix/shell.nix index 335c731f..ec9652b2 100644 --- a/nix/shell.nix +++ b/nix/shell.nix @@ -1,7 +1,10 @@ -{ pkgs, inputs, ... }: +{ pkgs, ... }: pkgs.mkShell { packages = with pkgs; [ + rustToolchain + rust-analyzer + nodePackages.cspell file @@ -15,18 +18,11 @@ pkgs.mkShell { zoxide ]; - buildInputs = pkgs.lib.optionals pkgs.stdenv.isDarwin ( - with pkgs.darwin.apple_sdk.frameworks; [ Foundation ] + buildInputs = with pkgs; lib.optionals stdenv.isDarwin ( + with darwin.apple_sdk.frameworks; [ Foundation ] ); - inputsFrom = [ - (inputs.devenv.lib.mkShell { - inherit inputs pkgs; - modules = [ - ({ pkgs, ... }: { - languages.rust.enable = true; - }) - ]; - }) - ]; + env = { + RUST_BACKTRACE = "1"; + }; } diff --git a/nix/yazi.nix b/nix/yazi.nix index f69978fd..1ab4368d 100644 --- a/nix/yazi.nix +++ b/nix/yazi.nix @@ -3,6 +3,7 @@ , lib , makeWrapper +, installShellFiles , stdenv , darwin @@ -34,7 +35,7 @@ rustPlatform.buildRustPackage { cargoLock.lockFile = ../Cargo.lock; - nativeBuildInputs = [ makeWrapper ]; + nativeBuildInputs = [ makeWrapper installShellFiles ]; buildInputs = lib.optionals stdenv.isDarwin ( with darwin.apple_sdk.frameworks; [ Foundation ] ); @@ -55,6 +56,10 @@ rustPlatform.buildRustPackage { '' wrapProgram $out/bin/yazi \ --prefix PATH : "${makeBinPath runtimePaths}" + installShellCompletion --cmd yazi \ + --bash ./config/completions/yazi.bash \ + --fish ./config/completions/yazi.fish \ + --zsh ./config/completions/_yazi ''; meta = with lib; {