From b8ba1ef0350d3cd3e356eb14f2b40864badb6ad6 Mon Sep 17 00:00:00 2001 From: Philipp Mandler Date: Sun, 19 Jan 2025 20:30:45 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Format=20nix=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flake.nix | 173 +++++++++++++++++++++++------------------- nix/pkgs/backend.nix | 5 +- nix/pkgs/frontend.nix | 112 +++++++++++++++------------ nix/pkgs/worker.nix | 5 +- 4 files changed, 166 insertions(+), 129 deletions(-) diff --git a/flake.nix b/flake.nix index 84cf8e0b..def73a8a 100644 --- a/flake.nix +++ b/flake.nix @@ -25,7 +25,8 @@ flake-utils.url = "github:numtide/flake-utils"; }; - outputs = { + outputs = + { nixpkgs, uv2nix, pyproject-nix, @@ -38,7 +39,8 @@ pythonPkgName = "python311"; in { - overlays.default = (final: prev: + overlays.default = ( + final: prev: let pkgs = import nixpkgs { system = final.system; @@ -48,11 +50,25 @@ in { transcribee-worker = import ./nix/pkgs/worker.nix { - inherit pkgs lib python uv2nix pyproject-nix pyproject-build-systems; + inherit + pkgs + lib + python + uv2nix + pyproject-nix + pyproject-build-systems + ; }; transcribee-backend = import ./nix/pkgs/backend.nix { - inherit pkgs lib python uv2nix pyproject-nix pyproject-build-systems; + inherit + pkgs + lib + python + uv2nix + pyproject-nix + pyproject-build-systems + ; }; transcribee-frontend = import ./nix/pkgs/frontend.nix { @@ -63,91 +79,96 @@ commitDate = self.lastModified; }; }; - }); + } + ); nixosModules.default = { nixpkgs.overlays = [ self.overlays.default ]; }; - } // - (flake-utils.lib.eachDefaultSystem - (system: - let - pkgs = import nixpkgs { - inherit system; - overlays = [ self.overlays.default ]; - }; - python = pkgs."${pythonPkgName}"; - - ld_packages = [ - pkgs.file # provides libmagic - - # for ctranslate2 - pkgs.stdenv.cc.cc.lib + } + // (flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ self.overlays.default ]; + }; + python = pkgs."${pythonPkgName}"; + + ld_packages = [ + pkgs.file # provides libmagic + + # for ctranslate2 + pkgs.stdenv.cc.cc.lib + ]; + in + { + packages = { + backend = pkgs.transcribee-backend; + worker = pkgs.transcribee-worker; + frontend = pkgs.transcribee-frontend; + }; + + formatter = pkgs.nixfmt-rfc-style; + + devShells.default = pkgs.mkShell { + packages = [ + python + pkgs.uv + python.pkgs.black + pkgs.poethepoet + + pkgs.overmind + pkgs.wait4x + pkgs.pre-commit + + pkgs.nodejs_20 + + # nix tooling + pkgs.nixpkgs-fmt + + # required by whispercppy + pkgs.cmake + + # required by pre-commit + pkgs.git + pkgs.ruff + + # required by psycopg2 + pkgs.openssl + + pkgs.ffmpeg + + # for automerge-py + pkgs.libiconv + pkgs.rustc + pkgs.cargo + + pkgs.icu.dev + + # Our database + pkgs.postgresql_14 + + # Our database2 ? + pkgs.redis + + pkgs.glibcLocales ]; - in - { - packages = { - backend = pkgs.transcribee-backend; - worker = pkgs.transcribee-worker; - frontend = pkgs.transcribee-frontend; - }; - - devShells.default = pkgs.mkShell { - packages = [ - python - pkgs.uv - python.pkgs.black - pkgs.poethepoet - pkgs.overmind - pkgs.wait4x - pkgs.pre-commit - - pkgs.nodejs_20 - - # nix tooling - pkgs.nixpkgs-fmt - - # required by whispercppy - pkgs.cmake - - # required by pre-commit - pkgs.git - pkgs.ruff - - # required by psycopg2 - pkgs.openssl - - pkgs.ffmpeg - - # for automerge-py - pkgs.libiconv - pkgs.rustc - pkgs.cargo - - pkgs.icu.dev - - # Our database - pkgs.postgresql_14 - - # Our database2 ? - pkgs.redis - - pkgs.glibcLocales - ]; - - shellHook = '' + shellHook = + '' unset PYTHONPATH export UV_PYTHON_DOWNLOADS=never export TRANSCRIBEE_DYLD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath ld_packages} export LD_LIBRARY_PATH=$LD_SEARCH_PATH:$TRANSCRIBEE_DYLD_LIBRARY_PATH - '' + pkgs.lib.optionalString pkgs.stdenv.isDarwin '' + '' + + pkgs.lib.optionalString pkgs.stdenv.isDarwin '' export CPPFLAGS="-I${pkgs.libcxx.dev}/include/c++/v1" # `dyld` needs to find the libraries export DYLD_LIBRARY_PATH=$LD_LIBRARY_PATH:$DYLD_LIBRARY_PATH ''; - }; - } - )); + }; + } + )); } diff --git a/nix/pkgs/backend.nix b/nix/pkgs/backend.nix index 690fdc3b..08592100 100644 --- a/nix/pkgs/backend.nix +++ b/nix/pkgs/backend.nix @@ -21,7 +21,7 @@ let pkgs.postgresql_14 ]; - buildInputs = (old.buildInputs or []) ++ [ + buildInputs = (old.buildInputs or [ ]) ++ [ pkgs.openssl ]; }); @@ -45,4 +45,5 @@ let ] ); -in pythonSet.mkVirtualEnv "transcribee-backend-env" workspace.deps.default +in +pythonSet.mkVirtualEnv "transcribee-backend-env" workspace.deps.default diff --git a/nix/pkgs/frontend.nix b/nix/pkgs/frontend.nix index 3f4caa2b..a1fcc3d7 100644 --- a/nix/pkgs/frontend.nix +++ b/nix/pkgs/frontend.nix @@ -4,58 +4,72 @@ versionInfo, ... }: -pkgs.buildNpmPackage (lib.fix (self: { - pname = "transcribee-frontend"; - version = "0.1.0"; - src = ../../frontend; +pkgs.buildNpmPackage ( + lib.fix (self: { + pname = "transcribee-frontend"; + version = "0.1.0"; + src = ../../frontend; - nativeBuildInputs = [ pkgs.git ]; + nativeBuildInputs = [ pkgs.git ]; - npmDeps = pkgs.importNpmLock { - npmRoot = self.src; - packageSourceOverrides = let - gitDeps = (lib.attrsets.filterAttrs (name: pkgInfo: - (lib.hasPrefix "node_modules/" name) - && (lib.hasPrefix "git" (pkgInfo.resolved or ""))) - (lib.importJSON (self.src + "/package-lock.json")).packages); - in lib.attrsets.mapAttrs (name: pkgInfo: - let - src = builtins.fetchGit { - url = "https" + lib.removePrefix "git+ssh" pkgInfo.resolved; - rev = lib.last (lib.splitString "#" pkgInfo.resolved); - allRefs = true; - }; - pname = lib.removePrefix "node_modules/" name; - thePkg = pkgs.buildNpmPackage { - inherit src pname; - version = pkgInfo.version; - npmDeps = pkgs.importNpmLock { npmRoot = src; }; - npmConfigHook = pkgs.importNpmLock.npmConfigHook; - installPhase = '' - mkdir $out - npm pack --pack-destination=$out - mv $out/*.tgz $out/package.tgz - ''; - }; - in "${thePkg}/package.tgz") gitDeps; - }; + npmDeps = pkgs.importNpmLock { + npmRoot = self.src; + packageSourceOverrides = + let + gitDeps = ( + lib.attrsets.filterAttrs ( + name: pkgInfo: + (lib.hasPrefix "node_modules/" name) && (lib.hasPrefix "git" (pkgInfo.resolved or "")) + ) (lib.importJSON (self.src + "/package-lock.json")).packages + ); + in + lib.attrsets.mapAttrs ( + name: pkgInfo: + let + src = builtins.fetchGit { + url = "https" + lib.removePrefix "git+ssh" pkgInfo.resolved; + rev = lib.last (lib.splitString "#" pkgInfo.resolved); + allRefs = true; + }; + pname = lib.removePrefix "node_modules/" name; + thePkg = pkgs.buildNpmPackage { + inherit src pname; + version = pkgInfo.version; + npmDeps = pkgs.importNpmLock { npmRoot = src; }; + npmConfigHook = pkgs.importNpmLock.npmConfigHook; + installPhase = '' + mkdir $out + npm pack --pack-destination=$out + mv $out/*.tgz $out/package.tgz + ''; + }; + in + "${thePkg}/package.tgz" + ) gitDeps; + }; - preBuild = let - versionExports = ([ ] - ++ (lib.optional (versionInfo ? commitHash) ''export COMMIT_HASH="${versionInfo.commitHash}"'') - ++ (lib.optional (versionInfo ? commitDate) ''export COMMIT_DATE="$(date -d @${builtins.toString versionInfo.commitDate} --iso-8601=s)"'') - ); - in '' - ${lib.concatStringsSep "\n" versionExports} - ''; + preBuild = + let + versionExports = ( + [ ] + ++ (lib.optional (versionInfo ? commitHash) ''export COMMIT_HASH="${versionInfo.commitHash}"'') + ++ (lib.optional ( + versionInfo ? commitDate + ) ''export COMMIT_DATE="$(date -d @${builtins.toString versionInfo.commitDate} --iso-8601=s)"'') + ); + in + '' + ${lib.concatStringsSep "\n" versionExports} + ''; - installPhase = '' - runHook preInstall - cp -r dist $out - runHook postInstall - ''; + installPhase = '' + runHook preInstall + cp -r dist $out + runHook postInstall + ''; - npmBuildScript = "build"; + npmBuildScript = "build"; - npmConfigHook = pkgs.importNpmLock.npmConfigHook; -})) + npmConfigHook = pkgs.importNpmLock.npmConfigHook; + }) +) diff --git a/nix/pkgs/worker.nix b/nix/pkgs/worker.nix index 49f2c03c..b28c3bb6 100644 --- a/nix/pkgs/worker.nix +++ b/nix/pkgs/worker.nix @@ -36,7 +36,7 @@ let pyicu = prev.pyicu.overrideAttrs (old: { nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.icu.dev - (final.resolveBuildSystem {}) + (final.resolveBuildSystem { }) ]; }); }; @@ -58,4 +58,5 @@ let ] ); -in pythonSet.mkVirtualEnv "transcribee-worker-env" workspace.deps.default +in +pythonSet.mkVirtualEnv "transcribee-worker-env" workspace.deps.default