feat: add a devshell based on vanilla Nix

Signed-off-by: Raito Bezarius <masterancpp@gmail.com>
This commit is contained in:
raito 2024-10-30 18:42:25 +01:00
parent 431376f887
commit 3e8152fe99
13 changed files with 250 additions and 162 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use nix

2
.gitignore vendored
View file

@ -10,3 +10,5 @@ result
target
*.nix.orig
*.nix.rej
.rabbitmq-data
.checkouts

2
Procfile Normal file
View file

@ -0,0 +1,2 @@
amqp-server: rabbitmq-server
pastebin-worker: cargo run --bin pastebin-worker -- dev.config.json

View file

@ -1,6 +1,88 @@
(import
(fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/0f9255e01c2351cc7d116c072cb317785dd33b33.tar.gz";
sha256 = "0m9grvfsbwmvgwaxvdzv6cmyvjnlww004gfxjvcl806ndqaxzy4j";
})
{ src = ./.; }).defaultNix.packages.${builtins.currentSystem}
{ sources ? import ./npins, pkgs ? import sources.nixpkgs {} }:
{
packages = {
ofborg = pkgs.rustPlatform.buildRustPackage {
name = "ofborg";
src = pkgs.nix-gitignore.gitignoreSource [ ] ./.;
nativeBuildInputs = with pkgs; [
pkg-config
rustPackages.clippy
];
buildInputs = with pkgs; [
] ++ lib.optionals pkgs.stdenv.isDarwin (with pkgs; [
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.CoreFoundation
]);
preBuild = ''
cargo clippy
'';
doCheck = false; # Tests require access to a /nix/ and a nix daemon
checkInputs = with pkgs; [
lix
];
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"hubcaps-0.6.2" = "sha256-yyHOCxUsehvtYfttRY4T9TDrJhSKGpJRa/SX3Sd1TNc=";
};
};
};
};
shell = pkgs.mkShell {
name = "ofborg-devenv";
RABBITMQ_CONFIG_FILE = pkgs.writeText "rabbitmq-dev.conf" ''
listeners.tcp.1=:::5672
'';
TRIVIAL_PASSWORD = pkgs.writeText "trivial-password.txt" "test";
RABBITMQ_LOGS = "-";
packages = with pkgs; [
lix
gitFull
nix-prefetch-git
rustc
cargo
clippy
rustfmt
pkg-config
rabbitmq-server
hivemind
];
postHook = ''
checkPhase() (
cd "${builtins.toString ./.}/ofborg"
set -x
cargo fmt
git diff --exit-code
cargofmtexit=$?
cargo clippy
cargoclippyexit=$?
cargo build && cargo test
cargotestexit=$?
sum=$((cargofmtexit + cargoclippyexit + cargotestexit))
exit $sum
)
'';
shellHook = ''
export RABBITMQ_MNESIA_BASE="$(git rev-parse --show-toplevel)/.rabbitmq-data";
export STATE_DIRECTORY="$(git rev-parse --show-toplevel)/.ofborg-state"
mkdir -p "$STATE_DIRECTORY"
mkdir -p "$RABBITMQ_MNESIA_BASE"
'';
RUSTFLAGS = "-D warnings";
RUST_BACKTRACE = "1";
RUST_LOG = "ofborg=debug";
NIX_PATH = "nixpkgs=${pkgs.path}";
};
}

26
dev.config.json Normal file
View file

@ -0,0 +1,26 @@
{
"runner": {
"identity": "dev"
},
"checkout": {
"root": "$STATE_DIRECTORY/.checkouts"
},
"nix": {
"system": "x86_64-linux",
"remote": "daemon",
"build_timeout_seconds": 1800
},
"rabbitmq": {
"ssl": false,
"host": "127.0.0.1",
"virtualhost": "/",
"username": "test",
"password_file": "$TRIVIAL_PASSWORD"
},
"feedback": {
"full_logs": true
},
"pastebin": {
"root": "$STATE_DIRECTORY/.pastebins"
}
}

View file

@ -1,27 +0,0 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1720031269,
"narHash": "sha256-rwz8NJZV+387rnWpTYcXaRNvzUSnnF9aHONoJIYmiUQ=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "9f4128e00b0ae8ec65918efeba59db998750ead6",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

115
flake.nix
View file

@ -1,115 +0,0 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};
outputs =
{ self
, nixpkgs
, ...
}@inputs:
let
supportedSystems = [ "aarch64-darwin" "x86_64-darwin" "x86_64-linux" "aarch64-linux" ];
forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system);
in
{
devShell = forAllSystems (system: inputs.self.devShells.${system}.default);
devShells = forAllSystems
(system:
let
pkgs = import nixpkgs {
inherit system;
};
in
{
default = pkgs.mkShell {
name = "gh-event-forwarder";
nativeBuildInputs = with pkgs; [
nix # so in --pure mode we actually find the "correct" nix
bash
nix-prefetch-git
rustc
cargo
clippy
rustfmt
pkg-config
git
];
buildInputs = with pkgs; [
] ++ lib.optionals stdenv.isDarwin [ darwin.Security libiconv ];
postHook = ''
checkPhase() (
cd "${builtins.toString ./.}/ofborg"
set -x
cargo fmt
git diff --exit-code
cargofmtexit=$?
cargo clippy
cargoclippyexit=$?
cargo build && cargo test
cargotestexit=$?
sum=$((cargofmtexit + cargoclippyexit + cargotestexit))
exit $sum
)
'';
RUSTFLAGS = "-D warnings";
RUST_BACKTRACE = "1";
RUST_LOG = "ofborg=debug";
NIX_PATH = "nixpkgs=${pkgs.path}";
};
});
packages = forAllSystems (system:
let
pkgs = import nixpkgs {
inherit system;
};
pkg = pkgs.rustPlatform.buildRustPackage {
name = "ofborg";
src = pkgs.nix-gitignore.gitignoreSource [ ] ./.;
nativeBuildInputs = with pkgs; [
pkg-config
pkgs.rustPackages.clippy
];
buildInputs = with pkgs; [
] ++ lib.optionals pkgs.stdenv.isDarwin (with pkgs; [
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.CoreFoundation
]);
preBuild = ''
cargo clippy
'';
doCheck = false; # Tests require access to a /nix/ and a nix daemon
checkInputs = with pkgs; [
nix
];
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"hubcaps-0.6.2" = "sha256-yyHOCxUsehvtYfttRY4T9TDrJhSKGpJRa/SX3Sd1TNc=";
};
};
};
in
{
default = pkg;
ofborg = pkg;
});
hydraJobs = {
buildRs = forAllSystems (system: self.packages.${system}.ofborg);
};
};
}

80
npins/default.nix Normal file
View file

@ -0,0 +1,80 @@
# Generated by npins. Do not modify; will be overwritten regularly
let
data = builtins.fromJSON (builtins.readFile ./sources.json);
version = data.version;
mkSource =
spec:
assert spec ? type;
let
path =
if spec.type == "Git" then
mkGitSource spec
else if spec.type == "GitRelease" then
mkGitSource spec
else if spec.type == "PyPi" then
mkPyPiSource spec
else if spec.type == "Channel" then
mkChannelSource spec
else
builtins.throw "Unknown source type ${spec.type}";
in
spec // { outPath = path; };
mkGitSource =
{
repository,
revision,
url ? null,
hash,
branch ? null,
...
}:
assert repository ? type;
# At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
# In the latter case, there we will always be an url to the tarball
if url != null then
(builtins.fetchTarball {
inherit url;
sha256 = hash;
})
else
assert repository.type == "Git";
let
urlToName =
url: rev:
let
matched = builtins.match "^.*/([^/]*)(\\.git)?$" repository.url;
short = builtins.substring 0 7 rev;
appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else "";
in
"${if matched == null then "source" else builtins.head matched}${appendShort}";
name = urlToName repository.url revision;
in
builtins.fetchGit {
url = repository.url;
rev = revision;
inherit name;
narHash = hash;
};
mkPyPiSource =
{ url, hash, ... }:
builtins.fetchurl {
inherit url;
sha256 = hash;
};
mkChannelSource =
{ url, hash, ... }:
builtins.fetchTarball {
inherit url;
sha256 = hash;
};
in
if version == 4 then
builtins.mapAttrs (_: mkSource) data.pins
else
throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"

11
npins/sources.json Normal file
View file

@ -0,0 +1,11 @@
{
"pins": {
"nixpkgs": {
"type": "Channel",
"name": "nixpkgs-unstable",
"url": "https://releases.nixos.org/nixpkgs/nixpkgs-24.11pre698484.30c9efeef01e/nixexprs.tar.xz",
"hash": "079c53r3wryasv3ghyi2da9ipxh1bfh2pl63yj447bilh5ghjhhz"
}
},
"version": 4
}

View file

@ -48,9 +48,7 @@ fn main() -> Result<(), Box<dyn Error>> {
})?;
let handle = easylapin::WorkerChannel(chan).consume(
tasks::pastebin_collector::PastebinCollector::new(
cfg.pastebin.clone().path
),
tasks::pastebin_collector::PastebinCollector::new(cfg.pastebin.clone().root),
easyamqp::ConsumeConfig {
queue: queue_name.clone(),
consumer_tag: format!("{}-pastebin-worker", cfg.whoami()),

View file

@ -32,7 +32,8 @@ pub struct FeedbackConfig {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PastebinConfig {
pub path: PathBuf,
#[serde(deserialize_with = "deserialize_and_expand_pathbuf")]
pub root: PathBuf,
// max_disk_size?
// auto_expiry?
}
@ -43,7 +44,7 @@ pub struct RabbitMqConfig {
pub host: String,
pub virtualhost: Option<String>,
pub username: String,
pub password_file: PathBuf,
pub password_file: Option<PathBuf>,
}
#[derive(Serialize, Deserialize, Debug)]
@ -96,6 +97,7 @@ pub struct RunnerConfig {
#[derive(Serialize, Deserialize, Debug)]
pub struct CheckoutConfig {
#[serde(deserialize_with = "deserialize_and_expand_string")]
pub root: String,
}
@ -165,9 +167,15 @@ impl Config {
impl RabbitMqConfig {
pub fn as_uri(&self) -> Result<String, std::io::Error> {
let password_file_as_str = self.password_file.to_string_lossy();
let expanded_password_file = shellexpand::env(&password_file_as_str).expect("Failed to expand the password-file configuration string");
let password = std::fs::read_to_string(expanded_password_file.as_ref())?;
let password;
if let Some(password_file) = &self.password_file {
let password_file_as_str = password_file.to_string_lossy();
let expanded_password_file = shellexpand::env(&password_file_as_str)
.expect("Failed to expand the password-file configuration string");
password = std::fs::read_to_string(expanded_password_file.as_ref())?;
} else {
password = "".to_owned();
}
let uri = format!(
"{}://{}:{}@{}/{}",
if self.ssl { "amqps" } else { "amqp" },
@ -283,3 +291,25 @@ where
deserializer.deserialize_any(StringOrVec(PhantomData))
}
fn deserialize_and_expand_string<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let raw_literal: String = Deserialize::deserialize(deserializer)?;
Ok(shellexpand::env(&raw_literal)
.map_err(|_| serde::de::Error::custom("failed to expand the variable in a string"))?
.into_owned())
}
fn deserialize_and_expand_pathbuf<'de, D>(deserializer: D) -> Result<PathBuf, D::Error>
where
D: Deserializer<'de>,
{
let raw_literal: PathBuf = Deserialize::deserialize(deserializer)?;
Ok(PathBuf::from(
shellexpand::env(&raw_literal.to_string_lossy())
.map_err(|_| serde::de::Error::custom("failed to expand the variable in a path"))?
.into_owned(),
))
}

3
scripts/init-account.sh Normal file
View file

@ -0,0 +1,3 @@
rabbitmqctl add_user test
rabbitmqctl clear_password test
rabbitmqctl set_globally_permissions test ".*" ".*" ".*"

View file

@ -1,6 +1 @@
(import
(fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/0f9255e01c2351cc7d116c072cb317785dd33b33.tar.gz";
sha256 = "0m9grvfsbwmvgwaxvdzv6cmyvjnlww004gfxjvcl806ndqaxzy4j";
})
{ src = ./.; }).shellNix
(import ./. { }).shell