Skip to content

Commit

Permalink
Add configurable flake deployment component (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaiser authored May 1, 2024
1 parent 139b561 commit c7fc361
Showing 1 changed file with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.nikolaiser.biser.nix

import besom.*
import besom.api.command
import besom.internal.RegistersOutputs
import besom.json.JsonWriter
import scala.concurrent.Future
import Pulumi.given_ExecutionContext

case class ConfigurableFlakeDeployment(
flakeDir: Output[String]
)(using ComponentBase)
extends ComponentResource
derives RegistersOutputs

object ConfigurableFlakeDeployment:

def apply[A: JsonWriter](using Context)(
flakeRepository: String,
flakeOutput: String,
config: A,
targetHost: String, // including usrname@
name: String,
configLocation: String = "./config.json",
options: ComponentResourceOptions = ComponentResourceOptions()
): Output[ConfigurableFlakeDeployment] =
component(
s"""biser-flake-deployment-$name""",
"biser:nix:ConfigurableFlakeDeployment",
options
) {
val jsonConfig = summon[JsonWriter[A]].write(config).toString

val deployment = for {

flakeDir <- command.local
.Command(
s"biser-flake-deployment-$name-flakedir",
command.local.CommandArgs(
create =
s"mkdir -p '$$XDG_STATE_HOME/biser/flakes/$name' && echo '$$XDG_STATE_HOME/biser/flakes/$name'",
delete = s"rm -rf '$$XDG_STATE_HOME/biser/flakes/$name'"
)
)
.stdout

_ <- command.local
.Command(
s"biser-flake-deployment-$flakeRepository-build",
command.local.CommandArgs(
create =
s"""git clone --depth=1 $flakeRepository . && echo '$jsonConfig' > '$configLocation' && nixos-rebuild switch < /dev/null --use-remote-sudo --target-host $targetHost --show-trace --flake ".#$flakeOutput"""",
dir = flakeDir,
environment = Map(
"NIX_SSHOPTS" -> "-t -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
)
)
)
.stdout

} yield flakeDir

ConfigurableFlakeDeployment(deployment)
}

0 comments on commit c7fc361

Please sign in to comment.