From 5c30371a9f594fa34d8714bee51663a075fbd11b Mon Sep 17 00:00:00 2001 From: winston Date: Mon, 27 Nov 2023 17:49:55 +0100 Subject: [PATCH] build(nix): improve nix build, step 1 refs: #218 --- .envrc | 2 +- .yarnrc.yml | 4 ++ default.nix | 57 ---------------- flake.nix | 4 +- nix/default.nix | 82 ++++++++++++++++++++++++ shell.nix => nix/shell.nix | 2 + yarn-project.nix => nix/yarn-project.nix | 4 +- package.json | 2 +- tsup.config.ts | 8 ++- 9 files changed, 100 insertions(+), 65 deletions(-) delete mode 100644 default.nix create mode 100644 nix/default.nix rename shell.nix => nix/shell.nix (82%) rename yarn-project.nix => nix/yarn-project.nix (99%) diff --git a/.envrc b/.envrc index 7b616241a..f1e3a46d2 100644 --- a/.envrc +++ b/.envrc @@ -1,2 +1,2 @@ -use nix +use nix ./nix/shell.nix layout node diff --git a/.yarnrc.yml b/.yarnrc.yml index 07a8a70b7..020e50e8e 100644 --- a/.yarnrc.yml +++ b/.yarnrc.yml @@ -12,4 +12,8 @@ plugins: path: .yarn/plugins/yarn-plugin-nixify.cjs spec: "https://raw.githubusercontent.com/stephank/yarn-plugin-nixify/main/dist/yarn-plugin-nixify.js" +# yarn-plugin-nixify options +generateDefaultNix: false +nixExprPath: nix/yarn-project.nix + yarnPath: .yarn/releases/yarn-4.0.2.cjs diff --git a/default.nix b/default.nix deleted file mode 100644 index 1b4daae17..000000000 --- a/default.nix +++ /dev/null @@ -1,57 +0,0 @@ -{ - pkgs ? import {}, - accentColor ? "mauve", - boldKeywords ? true, - italicComments ? true, - italicKeywords ? true, - extraBordersEnabled ? false, - workbenchMode ? "default", - bracketMode ? "rainbow", - colorOverrides ? {}, - customUIColors ? {}, -} @ inputs: let - inherit (pkgs) lib; - - packageJSON = builtins.fromJSON (builtins.readFile ./package.json); - properties = (builtins.head packageJSON.contributes.configuration).properties; - validAccents = properties."catppuccin.accentColor".enum; - validWorkbenchModes = properties."catppuccin.workbenchMode".enum; - validBracketModes = properties."catppuccin.bracketMode".enum; - - inherit (packageJSON) name version; - pname = "${name}-${version}"; - - options = builtins.removeAttrs inputs ["pkgs"]; - project = - (pkgs.callPackage ./yarn-project.nix {} { - src = pkgs.nix-gitignore.gitignoreSource [] ./.; - }) - .overrideAttrs (old: { - # check in the ./themes/.flag so it doesn't prompt for initial rebuilds - patchPhase = '' - printf "\n!themes/.flag\n" >> .vscodeignore - ''; - buildPhase = '' - yarn compile - yarn compile:theme '${builtins.toJSON options}' - touch ./themes/.flag - yarn package - ''; - installPhase = '' - mkdir -p $out - # rename the file extension to zip for the buildVSCodeMarketplaceExtension builder - mv catppuccin-vsc-*.vsix $out/catppuccin-vsc.zip - ''; - }); -in - (lib.checkListOfEnum "${pname}: accent" validAccents [accentColor]) - (lib.checkListOfEnum "${pname}: workbenchMode" validWorkbenchModes [workbenchMode]) - (lib.checkListOfEnum "${pname}: bracketMode" validBracketModes [bracketMode]) - pkgs.vscode-utils.buildVscodeMarketplaceExtension { - vsix = "${project.outPath}/catppuccin-vsc.zip"; - mktplcRef = { - # lowercase since it is used in the pname - publisher = "catppuccin"; - inherit name version; - }; - } diff --git a/flake.nix b/flake.nix index 9e76c7c8c..b2f9e71be 100644 --- a/flake.nix +++ b/flake.nix @@ -9,7 +9,7 @@ pkgs = nixpkgs.legacyPackages.${system}; in rec { default = catppuccin-vsc; - catppuccin-vsc = pkgs.callPackage ./. {}; + catppuccin-vsc = pkgs.callPackage ./nix {}; }); overlays.default = final: prev: let @@ -32,7 +32,7 @@ devShells = forAllSystems (system: let pkgs = nixpkgs.legacyPackages.${system}; in { - default = import ./shell.nix {inherit pkgs;}; + default = import ./nix/shell.nix {inherit pkgs;}; }); }; } diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 000000000..8d496d0c5 --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,82 @@ +{ + pkgs ? import {}, + accentColor ? "mauve", + boldKeywords ? true, + italicComments ? true, + italicKeywords ? true, + extraBordersEnabled ? false, + workbenchMode ? "default", + bracketMode ? "rainbow", + colorOverrides ? {}, + customUIColors ? {}, +} @ inputs: let + inherit (pkgs) lib; + + packageJSON = builtins.fromJSON (builtins.readFile ../package.json); + properties = (builtins.head packageJSON.contributes.configuration).properties; + validAccents = properties."catppuccin.accentColor".enum; + validWorkbenchModes = properties."catppuccin.workbenchMode".enum; + validBracketModes = properties."catppuccin.bracketMode".enum; + + inherit (packageJSON) name version; + pname = "${name}-${version}"; + + options = builtins.removeAttrs inputs ["pkgs"]; + src = pkgs.nix-gitignore.gitignoreSource [] ../.; + builder = pkgs.callPackage ./yarn-project.nix {} { + inherit src; + overrideAttrs = { + pname = "${pname}-build"; + buildPhase = '' + runHook preBuild + yarn compile + runHook postBuild + ''; + installPhase = '' + runHook preInstall + mkdir -p $out + cp -r dist/* $out + runHook postInstall + ''; + }; + }; + extension = pkgs.stdenvNoCC.mkDerivation { + inherit name version pname src; + + buildInputs = [pkgs.nodejs pkgs.vsce pkgs.yarn]; + + # check in the ./themes/.flag so it doesn't prompt for initial rebuilds + patchPhase = '' + runHook prePatch + printf "\n!themes/.flag\n" >> .vscodeignore + runHook postPatch + ''; + buildPhase = '' + runHook preBuild + mkdir -p themes dist + cp -r ${builder}/* dist/ + touch ./themes/.flag + node dist/hooks/generateThemes.js '${builtins.toJSON options}' + vsce package --allow-star-activation --yarn + runHook postBuild + ''; + installPhase = '' + runHook preInstall + mkdir -p $out + # rename the file extension to zip for the buildVSCodeMarketplaceExtension builder + mv catppuccin-vsc-*.vsix $out/catppuccin-vsc.zip + runHook postInstall + ''; + }; +in + (lib.checkListOfEnum "${pname}: accent" validAccents [accentColor]) + (lib.checkListOfEnum "${pname}: workbenchMode" validWorkbenchModes [workbenchMode]) + (lib.checkListOfEnum "${pname}: bracketMode" validBracketModes [bracketMode]) + pkgs.vscode-utils.buildVscodeMarketplaceExtension { + vsix = "${extension.outPath}/catppuccin-vsc.zip"; + mktplcRef = { + # lowercase since it is used in the pname + publisher = "catppuccin"; + inherit name version; + }; + } diff --git a/shell.nix b/nix/shell.nix similarity index 82% rename from shell.nix rename to nix/shell.nix index 3e4c4effe..d2ab8c7c0 100644 --- a/shell.nix +++ b/nix/shell.nix @@ -5,5 +5,7 @@ in buildInputs = [ nodejs (pkgs.yarn.override {inherit nodejs;}) + pkgs.alejandra + pkgs.nil ]; } diff --git a/yarn-project.nix b/nix/yarn-project.nix similarity index 99% rename from yarn-project.nix rename to nix/yarn-project.nix index b6aad9e3a..6c731a2d8 100644 --- a/yarn-project.nix +++ b/nix/yarn-project.nix @@ -6,10 +6,10 @@ let - yarnBin = ./.yarn/releases/yarn-4.0.2.cjs; + yarnBin = ../.yarn/releases/yarn-4.0.2.cjs; cacheFolder = ".yarn/cache"; - lockfile = ./yarn.lock; + lockfile = ../yarn.lock; # Call overrideAttrs on a derivation if a function is provided. optionalOverride = fn: drv: diff --git a/package.json b/package.json index d01a5722c..cfc89ac69 100644 --- a/package.json +++ b/package.json @@ -201,7 +201,7 @@ "chromatic": "chromatic --exit-zero-on-changes --exit-once-uploaded --project-token chpt_e3cba49738d7554", "compile": "tsup", "compile:theme": "tsx src/hooks/generateThemes.ts", - "dev": "DEBUG=1 yarn build", + "dev": "yarn compile:theme && yarn compile --env.NODE_ENV development", "lint": "eslint . --ext .ts", "package": "vsce package --allow-star-activation --yarn", "postbuild": "prettier -w ./themes", diff --git a/tsup.config.ts b/tsup.config.ts index 96a7c98bf..a473e6ddf 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -1,9 +1,13 @@ import { defineConfig } from "tsup"; +const dev = process.env.NODE_ENV === "development"; + export default defineConfig({ - entry: ["src/browser.ts", "src/main.ts"], + entry: ["src/browser.ts", "src/main.ts", "src/hooks/generateThemes.ts"], external: ["vscode"], - sourcemap: true, + sourcemap: dev, + minify: !dev, target: "node16", clean: true, + splitting: true, });