diff --git a/flake.nix b/flake.nix index f48f3c5..c0728ed 100644 --- a/flake.nix +++ b/flake.nix @@ -7,7 +7,9 @@ }; outputs = { self, nixpkgs, rust-overlay }@inputs: - let + let + inherit (nixpkgs) lib; + systems = [ "x86_64-linux" "aarch64-linux" @@ -15,6 +17,7 @@ "aarch64-darwin" "armv7l-linux" ]; + forAllSystems = f: nixpkgs.lib.genAttrs systems (system: let pkgs = import nixpkgs { inherit system; @@ -27,6 +30,16 @@ toolchain = rust-bin.stable.latest.default; in f system pkgs toolchain); in { + + apps = let + mkApp = program: { type = "app"; program = toString program; }; + in forAllSystems (system: pkgs: _: { + mysqladm-rs = mkApp (lib.getExe self.packages.${system}.mysqladm-rs); + coverage = mkApp (pkgs.writeScript "mysqladm-rs-coverage" '' + ${lib.getExe pkgs.python3} -m http.server -d "${self.packages.${system}.coverage}/html/src" + ''); + }); + devShell = forAllSystems (system: pkgs: toolchain: pkgs.mkShell { nativeBuildInputs = with pkgs; [ toolchain @@ -36,5 +49,33 @@ RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library"; }); + + overlays = { + default = self.overlays.mysqladm-rs; + greg-ng = final: prev: { + inherit (self.packages.${prev.system}) mysqladm-rs; + }; + }; + + packages = let + cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); + cargoLock = ./Cargo.lock; + src = builtins.filterSource (path: type: let + baseName = baseNameOf (toString path); + in !(lib.any (b: b) [ + (!(lib.cleanSourceFilter path type)) + (baseName == "target" && type == "directory") + (baseName == "nix" && type == "directory") + (baseName == "flake.nix" && type == "regular") + (baseName == "flake.lock" && type == "regular") + ])) ./.; + in forAllSystems (system: pkgs: _: { + default = self.packages.${system}.mysqladm-rs; + mysqladm-rs = pkgs.callPackage ./nix/default.nix { inherit cargoToml cargoLock src; }; + coverage = pkgs.callPackage ./nix/coverage.nix { inherit cargoToml cargoLock src; }; + filteredSource = pkgs.runCommandLocal "filtered-source" { } '' + ln -s ${src} $out + ''; + }); }; } diff --git a/nix/coverage.nix b/nix/coverage.nix new file mode 100644 index 0000000..e8cf856 --- /dev/null +++ b/nix/coverage.nix @@ -0,0 +1,75 @@ +{ + lib +, stdenvNoCC +, rustPlatform +, cargoToml +, cargoLock +, src + +, rust-bin +, cargo-nextest +, grcov +}: + +stdenvNoCC.mkDerivation { + pname = "coverage-${cargoToml.package.name}"; + version = cargoToml.package.version; + inherit src; + + env = { + RUSTFLAGS = "-Cinstrument-coverage"; + LLVM_PROFILE_FILE = "target/coverage/%p-%m.profraw"; + }; + + cargoDeps = rustPlatform.importCargoLock { + lockFile = cargoLock; + }; + + nativeBuildInputs = [ + rustPlatform.cargoSetupHook + cargo-nextest + grcov + (rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override { + extensions = [ "llvm-tools-preview" ]; + })) + ]; + + buildPhase = '' + runHook preBuild + + export HOME="$(pwd)" + + cargo nextest run --all-features --release --no-fail-fast + + grcov \ + --source-dir . \ + --binary-path ./target/release/deps/ \ + --excl-start 'mod test* \{' \ + --ignore 'tests/*' \ + --ignore "*test.rs" \ + --ignore "*tests.rs" \ + --ignore "*github.com*" \ + --ignore "*libcore*" \ + --ignore "*rustc*" \ + --ignore "*liballoc*" \ + --ignore "*cargo*" \ + -t html \ + -o ./target/coverage/html \ + target/coverage/ + + runHook postBuild + ''; + + installPhase = '' + runHook preBuild + + mv target/coverage $out + + runHook postBuild + ''; + + meta = with lib; { + license = licenses.mit; + platforms = platforms.linux ++ platforms.darwin; + }; +} diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 0000000..ccc2cfe --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,22 @@ +{ + lib +, rustPlatform +, cargoToml +, cargoLock +, src +}: +let +in +rustPlatform.buildRustPackage { + pname = cargoToml.package.name; + version = cargoToml.package.version; + inherit src; + + cargoLock.lockFile = cargoLock; + + meta = with lib; { + license = licenses.mit; + platforms = platforms.linux ++ platforms.darwin; + mainProgram = (lib.head cargoToml.bin).name; + }; +}