diff --git a/.github/workflows/nix-build.yaml b/.github/workflows/nix-build.yaml index e831549..710d65c 100644 --- a/.github/workflows/nix-build.yaml +++ b/.github/workflows/nix-build.yaml @@ -18,7 +18,7 @@ jobs: - name: Install Nix uses: DeterminateSystems/nix-installer-action@main - name: Enable magic Nix cache - uses: DeterminateSystems/magic-nix-cache-action@main + uses: DeterminateSystems/flakehub-cache-action@main - name: Set version run: | if [ "${{ github.event_name }}" == "release" ]; then @@ -53,10 +53,12 @@ jobs: docker tag $IMAGE_TAG "${DOCKER_TAG}:$VERSION" docker tag $IMAGE_TAG "${DOCKER_TAG}:$MAJOR.$MINOR" docker tag $IMAGE_TAG "${DOCKER_TAG}:$MAJOR" + docker tag $IMAGE_TAG "${DOCKER_TAG}:latest" docker push "${DOCKER_TAG}:$VERSION" docker push "${DOCKER_TAG}:$MAJOR.$MINOR" docker push "${DOCKER_TAG}:$MAJOR" - else + docker push "${DOCKER_TAG}:latest" + elsf docker tag $IMAGE_TAG "${DOCKER_TAG}:nix-test" docker push "${DOCKER_TAG}:nix-test" fi diff --git a/flake.nix b/flake.nix index 7e90f80..d3d768b 100644 --- a/flake.nix +++ b/flake.nix @@ -1,7 +1,7 @@ { description = "AudioBookRequest"; - inputs = { + inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; flake-utils = { url = "github:numtide/flake-utils"; @@ -24,92 +24,114 @@ }; }; - outputs = { self, nixpkgs, flake-utils, pyproject-nix, uv2nix, pyproject-build-systems, ... }: flake-utils.lib.eachDefaultSystem (system: - let - pkgs = import nixpkgs { inherit system; }; - python = pkgs.python312; - workspace = uv2nix.lib.workspace.loadWorkspace { workspaceRoot = ./.; }; + outputs = + { + self, + nixpkgs, + flake-utils, + pyproject-nix, + uv2nix, + pyproject-build-systems, + ... + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { inherit system; }; + python = pkgs.python312; + workspace = uv2nix.lib.workspace.loadWorkspace { workspaceRoot = ./.; }; - pyprojectOverrides = final: prev: { - # both fastapi and fastapi-cli add the binary causing "FileCollisionError: Two or more packages are trying to provide the same file with different contents" - fastapi-cli = prev.fastapi-cli.overrideAttrs(_: { postInstall = "rm $out/bin/fastapi"; }); - }; + pyprojectOverrides = final: prev: { + # both fastapi and fastapi-cli add the binary causing "FileCollisionError: Two or more packages are trying to provide the same file with different contents" + fastapi-cli = prev.fastapi-cli.overrideAttrs (_: { + postInstall = "rm $out/bin/fastapi"; + }); + }; - overlay = workspace.mkPyprojectOverlay { sourcePreference = "wheel"; }; - pythonSet = (pkgs.callPackage pyproject-nix.build.packages { inherit python; }).overrideScope ( - pkgs.lib.composeManyExtensions [ - pyproject-build-systems.overlays.default - overlay - pyprojectOverrides - ] - ); - in - rec { - packages = rec { - # Creates a separate nix store virtualenv with the default dependencies (no devDependencies) - default = pythonSet.mkVirtualEnv "audiobookrequest-venv" workspace.deps.default; + overlay = workspace.mkPyprojectOverlay { sourcePreference = "wheel"; }; + pythonSet = (pkgs.callPackage pyproject-nix.build.packages { inherit python; }).overrideScope ( + pkgs.lib.composeManyExtensions [ + pyproject-build-systems.overlays.default + overlay + pyprojectOverrides + ] + ); + in + rec { + packages = rec { + # Creates a separate nix store virtualenv with the default dependencies (no devDependencies) + default = pythonSet.mkVirtualEnv "audiobookrequest-venv" workspace.deps.default; - - docker = - let - npmDeps = pkgs.importNpmLock.buildNodeModules { - package = pkgs.lib.importJSON ./package.json; - packageLock = pkgs.lib.importJSON ./package-lock.json; - nodejs = pkgs.nodejs_23; - }; - tw-init = pkgs.writeShellScriptBin "tw-init" '' - ln -s ${npmDeps}/node_modules node_modules - cp ${./static/tw.css} tw.css # copy over the file since tailwind looks for daisyui relative to the input file - ${pkgs.tailwindcss_4}/bin/tailwindcss -i tw.css -o $out/app/static/globals.css -m - ''; - run = pkgs.writeShellScriptBin "run" '' - ${default}/bin/alembic upgrade heads - ${default}/bin/fastapi run --port $ABR_APP__PORT - ''; - gitignore = pkgs.nix-gitignore.gitignoreSource [ ] ./.; - in - pkgs.dockerTools.buildImage { - name = "audiobookrequest"; - - copyToRoot = pkgs.buildEnv { - name = "test"; - paths = []; - postBuild = '' - mkdir -p $out/app/static - ${tw-init}/bin/tw-init - cp ${gitignore}/alembic.ini $out/app/alembic.ini - cp -r ${gitignore}/alembic $out/app/alembic - cp -r ${gitignore}/templates $out/app/templates - cp -r ${gitignore}/static $out/app/static - cp -r ${gitignore}/app $out/app/app - ''; - }; - - config = { - WorkingDir = "/app"; - Cmd = [ "${run}/bin/run" ]; - ExposedPorts = { - "8000/tcp" = {}; + docker = + let + npmDeps = pkgs.importNpmLock.buildNodeModules { + package = pkgs.lib.importJSON ./package.json; + packageLock = pkgs.lib.importJSON ./package-lock.json; + nodejs = pkgs.nodejs_23; + }; + tw-init = pkgs.writeShellScriptBin "tw-init" '' + ln -s ${npmDeps}/node_modules node_modules + cp -r ${gitignore}/templates templates # copy over to make sure tailwind generates the correct classes + cp ${./static/tw.css} tw.css # copy over the file since tailwind looks for daisyui relative to the input file + ${pkgs.tailwindcss_4}/bin/tailwindcss -i tw.css -o $out/app/static/globals.css -m + ''; + run = pkgs.writeShellScriptBin "run" '' + ${default}/bin/alembic upgrade heads + # exec is important to allow for C-c to work + exec ${default}/bin/fastapi run --port $ABR_APP__PORT + ''; + gitignore = pkgs.nix-gitignore.gitignoreSource [ ] ./.; + in + + pkgs.dockerTools.buildImage { + name = "audiobookrequest"; + + copyToRoot = pkgs.buildEnv { + name = "test"; + paths = [ ]; + postBuild = '' + mkdir -p $out/app/static + ${tw-init}/bin/tw-init + cp ${gitignore}/alembic.ini $out/app/alembic.ini + cp -r ${gitignore}/alembic $out/app/alembic + cp -r ${gitignore}/templates $out/app/templates + cp -r ${gitignore}/static/* $out/app/static + cp -r ${gitignore}/app $out/app/app + ''; + }; + + config = { + WorkingDir = "/app"; + Cmd = [ "${run}/bin/run" ]; + ExposedPorts = { + "8000/tcp" = { }; + }; + Env = [ + "ABR_APP__PORT=8000" + "ABR_APP__VERSION=${builtins.readFile ./static/version}" + ]; }; - Env = [ - "ABR_APP__PORT=8000" - "ABR_APP__VERSION=${builtins.readFile ./static/version}" - ]; }; }; - }; - - # What is run when we use `nix run . -- dev` - apps.default = { - type = "app"; - program = "${packages.default}/bin/fastapi"; - }; - - # Create a .venv and activates it. Allows for the venv to easily be selected in the editor for the python interpreter - devShells.default = pkgs.mkShell { + + # What is run when we use `nix run . -- dev` + apps.default = { + type = "app"; + program = "${packages.default}/bin/fastapi"; + }; + + # Create a .venv and activates it. Allows for the venv to easily be selected in the editor for the python interpreter + devShells.default = pkgs.mkShell { venvDir = ".venv"; - packages = with pkgs; [ nodejs_23 sqlite nodePackages.browser-sync python312Packages.venvShellHook uv ]; + packages = with pkgs; [ + nodejs_23 + sqlite + nodePackages.browser-sync + python312Packages.venvShellHook + uv + ]; postShellHook = "uv sync"; - }; - }); + }; + } + ); }