Skip to content

Commit

Permalink
Add cachix push stage (#11)
Browse files Browse the repository at this point in the history
Optionally configure auth token in CLI. Then, all builds will have their
full outputs (from omnix) pushed to cachix.
  • Loading branch information
srid authored Jan 30, 2025
1 parent ce7bd84 commit 133e944
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions nix/modules/flake-parts/devshell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ghciwatch
# vira extraBuildDepends from haskell.nix
git
cachix
inputs'.omnix.packages.default
];
};
Expand Down
8 changes: 7 additions & 1 deletion nix/modules/flake-parts/haskell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
vira = {
extraBuildDepends = [
pkgs.git
pkgs.cachix
inputs'.omnix.packages.default
];
stan = true;
Expand Down Expand Up @@ -76,7 +77,12 @@
process-compose."vira-dev" = {
settings = {
processes = {
haskell.command = "ghcid -c 'cabal repl exe:vira --flags=ghcid' -T :main";
# The cachix token here is for a dummy cache, managed by Srid.
haskell.command = ''
set -x
ghcid -c 'cabal repl exe:vira --flags=ghcid' -T Main.main \
--setup ':set args --repo-cachix-name scratch-vira-dev --repo-cachix-auth-token eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI5NDI4ZjhkZi1mZWM5LTQ1ZjctYjMzYi01MTFiZTljNTNkNjciLCJzY29wZXMiOiJjYWNoZSJ9.WgPWUSYIie2rUdfuPqHS5mxrkT0lc7KIN7QPBPH4H-U'
'';
tailwind = {
# Suppress tailwind output so only ghcid log comes through
command = "echo Running tailwind silently.; ${lib.getExe pkgs.haskellPackages.tailwind} -w -o ./static/tailwind.css './src/**/*.hs' > tailwind.log 2>&1";
Expand Down
29 changes: 29 additions & 0 deletions src/Vira/App/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ data RepoSettings = RepoSettings
-- ^ Repositories (git clone URL) to watch and build
, branchWhitelist :: Set Text
-- ^ Limit to building these branches to build
, cachix :: Maybe CachixSettings
-- ^ Cachix settings
}
deriving stock (Show)

data CachixSettings = CachixSettings
{ cachixName :: Text
-- ^ Name of the cachix cache
, authToken :: Text
-- ^ Auth token for the cachix cache
}
deriving stock (Show)

Expand Down Expand Up @@ -77,8 +87,27 @@ instance HasParser RepoSettings where
, name "branch-whitelist"
, value defaultBranchesToBuild
]
cachix <- optional $ subSettings "cachix"
pure RepoSettings {..}

instance HasParser CachixSettings where
settingsParser = withoutConfig $ do
cachixName <-
setting
[ reader str
, metavar "CACHIX_NAME"
, help "Name of the cachix cache"
, name "name"
]
authToken <-
setting
[ reader str
, metavar "CACHIX_AUTH_TOKEN"
, help "Auth token for the cachix cache"
, name "auth-token"
]
pure CachixSettings {..}

defaultRepos :: [Text]
defaultRepos =
[ "https://github.com/srid/emanote.git"
Expand Down
9 changes: 8 additions & 1 deletion src/Vira/Page/JobPage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Vira.Page.JobPage where

import Effectful (Eff)
import Effectful.Error.Static (throwError)
import Effectful.Process (CreateProcess (cwd), proc)
import Effectful.Process (CreateProcess (cwd), env, proc)
import Effectful.Reader.Dynamic (ask, asks)
import GHC.IO.Exception (ExitCode (..))
import Htmx.Servant.Response
Expand Down Expand Up @@ -90,13 +90,15 @@ triggerNewBuild repoName branchName = do
repo <- App.query (St.GetRepoByNameA repoName) >>= maybe (throwError $ err404 {errBody = "No such repo"}) pure
branch <- App.query (St.GetBranchByNameA repoName branchName) >>= maybe (throwError $ err404 {errBody = "No such branch"}) pure
log Info $ "Building commit " <> show (repoName, branch.headCommit)
mCachix <- asks $ App.cachix . App.repo . App.settings
asks App.supervisor >>= \supervisor -> do
job <- App.update $ St.AddNewJobA repoName branchName branch.headCommit supervisor.baseWorkDir
log Info $ "Added job " <> show job
let stages =
stageCreateProjectDir
:| stagesClone repo branch
<> [stageBuild]
<> maybe mempty (one . stageCachix) mCachix
Supervisor.startTask supervisor job.jobId job.jobWorkingDir stages $ \exitCode -> do
let status = case exitCode of
ExitSuccess -> St.JobFinished St.JobSuccess
Expand All @@ -114,3 +116,8 @@ triggerNewBuild repoName branchName = do
Omnix.omnixCiProcess
{ cwd = Just "project"
}
stageCachix cachix =
(proc "cachix" ["push", toString cachix.cachixName, "result"])
{ env = Just [("CACHIX_AUTH_TOKEN", toString cachix.authToken)]
, cwd = Just "project"
}

0 comments on commit 133e944

Please sign in to comment.