diff --git a/.github/workflows/nix.yml b/.github/workflows/nix.yml index 694809eca..92b65547a 100644 --- a/.github/workflows/nix.yml +++ b/.github/workflows/nix.yml @@ -40,5 +40,4 @@ jobs: run: nix build --override-input debugBuild github:boolean-option/true/6ecb49143ca31b140a5273f1575746ba93c3f698 -L .#mrf-tool - name: nix check overlay - run: cd test-overlay && nix build --no-write-lock-file -L .#kitsune - + run: cd nix/test-overlay && nix build --no-write-lock-file -L .#kitsune diff --git a/flake.nix b/flake.nix index fd4bea186..4fb205e4f 100644 --- a/flake.nix +++ b/flake.nix @@ -35,12 +35,12 @@ outputs = { self + , crane , devenv , flake-utils , nixpkgs - , rust-overlay - , crane , pnpm2nix + , rust-overlay , ... }@inputs: ( @@ -48,231 +48,32 @@ ( system: let - features = "--all-features"; overlays = [ (import rust-overlay) ]; pkgs = import nixpkgs { inherit overlays system; }; - stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv; - rustPlatform = pkgs.makeRustPlatform { - cargo = pkgs.rust-bin.stable.latest.minimal; - rustc = pkgs.rust-bin.stable.latest.minimal; - inherit stdenv; - }; - - craneLib = (crane.mkLib pkgs).overrideToolchain pkgs.rust-bin.stable.latest.minimal; - buildInputs = with pkgs; [ - ]; - - nativeBuildInputs = with pkgs; [ - ]; - - src = pkgs.lib.cleanSourceWith { - src = pkgs.lib.cleanSource ./.; - filter = - name: type: - let - baseName = baseNameOf (toString name); - in - !(baseName == "flake.lock" || pkgs.lib.hasSuffix ".nix" baseName); - }; - - commonArgs = - let - excludedPkgs = [ "example-mrf" "http-client-test" ]; - buildExcludeParam = pkgs.lib.strings.concatMapStringsSep " " (pkgName: "--exclude ${pkgName}"); - excludeParam = buildExcludeParam excludedPkgs; - in - { - inherit - src - stdenv - buildInputs - nativeBuildInputs - ; - - strictDeps = true; - - meta = { - description = "ActivityPub-federated microblogging"; - homepage = "https://joinkitsune.org"; - }; - - NIX_OUTPATH_USED_AS_RANDOM_SEED = "aaaaaaaaaa"; - CARGO_PROFILE = "dist"; - cargoExtraArgs = "--locked ${features} --workspace ${excludeParam}"; - } - // (pkgs.lib.optionalAttrs inputs.debugBuild.value { - # do a debug build, as `dev` is the default debug profile - CARGO_PROFILE = "dev"; - }); - - cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); - version = cargoToml.workspace.package.version; - - cargoArtifacts = craneLib.buildDepsOnly ( - commonArgs - // { - pname = "kitsune-workspace"; - src = craneLib.cleanCargoSource src; - doCheck = false; - } - ); in { formatter = pkgs.nixpkgs-fmt; - packages = rec { - default = main; - + packages = { devenv-up = self.devShells.${system}.default.config.procfileScript; + } // (import ./nix/packages.nix) { + inherit crane pkgs; - cli = craneLib.buildPackage ( - commonArgs - // { - pname = "kitsune-cli"; - cargoExtraArgs = commonArgs.cargoExtraArgs + " --bin kitsune-cli"; - inherit cargoArtifacts; - doCheck = false; - } - ); - - cli-docker = pkgs.dockerTools.buildLayeredImage { - name = "kitsune-cli"; - tag = "latest"; - contents = [ pkgs.dockerTools.caCertificates cli ]; - config.Cmd = [ "${cli}/bin/kitsune-cli" ]; - }; - - job-runner = craneLib.buildPackage ( - commonArgs - // { - pname = "kitsune-job-runner"; - cargoExtraArgs = commonArgs.cargoExtraArgs + " --bin kitsune-job-runner"; - inherit cargoArtifacts; - doCheck = false; - } - ); - - job-runner-docker = pkgs.dockerTools.buildLayeredImage { - name = "kitsune-job-runner"; - tag = "latest"; - contents = [ pkgs.dockerTools.caCertificates job-runner ]; - config.Cmd = [ "${job-runner}/bin/kitsune-job-runner" ]; - }; - - mrf-tool = craneLib.buildPackage ( - commonArgs - // { - pname = "mrf-tool"; - cargoExtraArgs = commonArgs.cargoExtraArgs + " --bin mrf-tool"; - inherit cargoArtifacts; - doCheck = false; - } - ); - - mrf-tool-docker = pkgs.dockerTools.buildLayeredImage { - name = "mrf-tool"; - tag = "latest"; - contents = [ mrf-tool ]; - config.Cmd = [ "${mrf-tool}/bin/mrf-tool" ]; - }; - - main = craneLib.buildPackage ( - commonArgs - // { - pname = "kitsune"; - cargoExtraArgs = commonArgs.cargoExtraArgs + " --bin kitsune"; - inherit cargoArtifacts; - doCheck = false; - } - ); - - main-docker = pkgs.dockerTools.buildLayeredImage { - name = "kitsune"; - tag = "latest"; - contents = [ pkgs.dockerTools.caCertificates main ]; - config.Cmd = [ "${main}/bin/kitsune" ]; - }; - - frontend = pnpm2nix.packages.${system}.mkPnpmPackage { - inherit src; - distDir = "kitsune-fe/build"; - installInPlace = true; - packageJSON = "${src}/kitsune-fe/package.json"; - script = "-C kitsune-fe build"; - }; - - website = pnpm2nix.packages.${system}.mkPnpmPackage { - inherit src; - distDir = "website/dist"; - installInPlace = true; - packageJSON = "${src}/website/package.json"; - script = "-C website build"; - }; + debugBuild = inputs.debugBuild; + mkPnpmPackage = pnpm2nix.packages.${system}.mkPnpmPackage; }; - devShells = rec { - default = backend; - - backend = devenv.lib.mkShell { - inherit pkgs inputs; - - modules = [ - ( - { pkgs, ... }: - { - packages = - with pkgs; - [ - cargo-insta - diesel-cli - rust-bin.stable.latest.default - ] - ++ buildInputs - ++ nativeBuildInputs; - - enterShell = '' - export PG_HOST=127.0.0.1 - export PG_PORT=5432 - [ -z "$DATABASE_URL" ] && export DATABASE_URL=postgres://$USER@$PG_HOST:$PG_PORT/$USER - - export REDIS_PORT=6379 - [ -z "$REDIS_URL" ] && export REDIS_URL="redis://127.0.0.1:$REDIS_PORT" - ''; - - services = { - postgres = { - enable = true; - listen_addresses = "127.0.0.1"; - }; - redis = { - package = pkgs.valkey; - enable = true; - }; - }; - } - ) - ]; - }; - - frontend = pkgs.mkShell { - buildInputs = with pkgs; [ - nodejs - nodePackages.svelte-language-server - nodePackages.typescript-language-server - pnpm - ]; - }; - }; + devShells = (import ./nix/devshells.nix) { inherit devenv pkgs inputs; }; } ) // { overlays = rec { default = kitsune; - kitsune = (import ./overlay.nix self); + kitsune = (import ./nix/overlay.nix self); }; nixosModules = rec { default = kitsune; - kitsune = (import ./module.nix); + kitsune = (import ./nix/module.nix); }; } ) diff --git a/nix/devshells.nix b/nix/devshells.nix new file mode 100644 index 000000000..86ec81a14 --- /dev/null +++ b/nix/devshells.nix @@ -0,0 +1,52 @@ +{ devenv, pkgs, inputs }: +rec { + default = backend; + + backend = devenv.lib.mkShell { + inherit pkgs inputs; + + modules = [ + ( + { pkgs, ... }: + { + packages = + with pkgs; + [ + cargo-insta + diesel-cli + rust-bin.stable.latest.default + ]; + + enterShell = '' + export PG_HOST=127.0.0.1 + export PG_PORT=5432 + [ -z "$DATABASE_URL" ] && export DATABASE_URL=postgres://$USER@$PG_HOST:$PG_PORT/$USER + + export REDIS_PORT=6379 + [ -z "$REDIS_URL" ] && export REDIS_URL="redis://127.0.0.1:$REDIS_PORT" + ''; + + services = { + postgres = { + enable = true; + listen_addresses = "127.0.0.1"; + }; + redis = { + package = pkgs.valkey; + enable = true; + }; + }; + } + ) + ]; + }; + + frontend = pkgs.mkShell { + buildInputs = with pkgs; [ + nodejs + nodePackages.svelte-language-server + nodePackages.typescript-language-server + pnpm + ]; + }; +} diff --git a/module.nix b/nix/module.nix similarity index 100% rename from module.nix rename to nix/module.nix diff --git a/overlay.nix b/nix/overlay.nix similarity index 100% rename from overlay.nix rename to nix/overlay.nix diff --git a/nix/packages.nix b/nix/packages.nix new file mode 100644 index 000000000..ecbf969dd --- /dev/null +++ b/nix/packages.nix @@ -0,0 +1,144 @@ +{ crane, debugBuild, pkgs, mkPnpmPackage }: +let + features = "--all-features"; + + stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv; + rustToolchain = pkgs.rust-bin.stable.latest.minimal; + + rustPlatform = pkgs.makeRustPlatform { + cargo = rustToolchain; + rustc = rustToolchain; + inherit stdenv; + }; + + craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; + + src = pkgs.lib.cleanSourceWith { + src = pkgs.lib.cleanSource ./..; + filter = + name: type: + let + baseName = baseNameOf (toString name); + in + !(baseName == "flake.lock" || pkgs.lib.hasSuffix ".nix" baseName); + }; + + commonArgs = + let + excludedPkgs = [ "example-mrf" "http-client-test" ]; + buildExcludeParam = pkgs.lib.strings.concatMapStringsSep " " (pkgName: "--exclude ${pkgName}"); + excludeParam = buildExcludeParam excludedPkgs; + in + { + inherit src stdenv; + + cargoExtraArgs = "--locked ${features} --workspace ${excludeParam}"; + strictDeps = true; + + CARGO_PROFILE = "dist"; + NIX_OUTPATH_USED_AS_RANDOM_SEED = "aaaaaaaaaa"; + } + // (pkgs.lib.optionalAttrs debugBuild.value { + # do a debug build, as `dev` is the default debug profile + CARGO_PROFILE = "dev"; + }); + + cargoToml = builtins.fromTOML (builtins.readFile ./../Cargo.toml); + version = cargoToml.workspace.package.version; + + cargoArtifacts = craneLib.buildDepsOnly ( + commonArgs + // { + pname = "kitsune-workspace"; + src = craneLib.cleanCargoSource src; + doCheck = false; + } + ); +in +rec { + default = main; + + cli = craneLib.buildPackage ( + commonArgs + // { + pname = "kitsune-cli"; + cargoExtraArgs = commonArgs.cargoExtraArgs + " --bin kitsune-cli"; + inherit cargoArtifacts; + doCheck = false; + } + ); + + cli-docker = pkgs.dockerTools.buildLayeredImage { + name = "kitsune-cli"; + tag = "latest"; + contents = [ pkgs.dockerTools.caCertificates cli ]; + config.Cmd = [ "${cli}/bin/kitsune-cli" ]; + }; + + job-runner = craneLib.buildPackage ( + commonArgs + // { + pname = "kitsune-job-runner"; + cargoExtraArgs = commonArgs.cargoExtraArgs + " --bin kitsune-job-runner"; + inherit cargoArtifacts; + doCheck = false; + } + ); + + job-runner-docker = pkgs.dockerTools.buildLayeredImage { + name = "kitsune-job-runner"; + tag = "latest"; + contents = [ pkgs.dockerTools.caCertificates job-runner ]; + config.Cmd = [ "${job-runner}/bin/kitsune-job-runner" ]; + }; + + mrf-tool = craneLib.buildPackage ( + commonArgs + // { + pname = "mrf-tool"; + cargoExtraArgs = commonArgs.cargoExtraArgs + " --bin mrf-tool"; + inherit cargoArtifacts; + doCheck = false; + } + ); + + mrf-tool-docker = pkgs.dockerTools.buildLayeredImage { + name = "mrf-tool"; + tag = "latest"; + contents = [ mrf-tool ]; + config.Cmd = [ "${mrf-tool}/bin/mrf-tool" ]; + }; + + main = craneLib.buildPackage ( + commonArgs + // { + pname = "kitsune"; + cargoExtraArgs = commonArgs.cargoExtraArgs + " --bin kitsune"; + inherit cargoArtifacts; + doCheck = false; + } + ); + + main-docker = pkgs.dockerTools.buildLayeredImage { + name = "kitsune"; + tag = "latest"; + contents = [ pkgs.dockerTools.caCertificates main ]; + config.Cmd = [ "${main}/bin/kitsune" ]; + }; + + frontend = mkPnpmPackage { + inherit src; + distDir = "kitsune-fe/build"; + installInPlace = true; + packageJSON = "${src}/kitsune-fe/package.json"; + script = "-C kitsune-fe build"; + }; + + website = mkPnpmPackage { + inherit src; + distDir = "website/dist"; + installInPlace = true; + packageJSON = "${src}/website/package.json"; + script = "-C website build"; + }; +} diff --git a/test-overlay/flake.nix b/nix/test-overlay/flake.nix similarity index 95% rename from test-overlay/flake.nix rename to nix/test-overlay/flake.nix index 15b4eb0ec..ad79806c2 100644 --- a/test-overlay/flake.nix +++ b/nix/test-overlay/flake.nix @@ -1,6 +1,6 @@ { inputs = { - kitsune-overlay.url = "./.."; + kitsune-overlay.url = "./../.."; kitsune-overlay.inputs.debugBuild.follows = "debugBuild"; nixpkgs.follows = "kitsune-overlay/nixpkgs"; flake-utils.follows = "kitsune-overlay/flake-utils";