-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configurable flake deployment component (#4)
- Loading branch information
1 parent
139b561
commit c7fc361
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/scala/com/nikolaiser/biser/nix/ConfigurableFlakeDeployment.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |