diff options
author | Matthias Beyer <matthias.beyer@ifm.com> | 2022-08-25 12:10:55 +0200 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@ifm.com> | 2022-09-09 14:19:04 +0200 |
commit | 4fa4856c5d21fdcfb37d5d6a0ca9853372f809f9 (patch) | |
tree | a7ff853ac0ca918bde371c74f7404b12c7f16214 | |
parent | 08a32f3294d65b4011e54838fc67c792cf8f3ff0 (diff) |
Add flake to build custom thin-edge binary
This patch adds a flake which is capable of building a custom
composition of thin-edge.io with a (possibly user-)defined set of
components.
Co-authored-by: Marcel Müller <m.mueller@ifm.com>
Signed-off-by: Marcel Müller <m.mueller@ifm.com>
Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r-- | lib/nix-thin-edge-builder/flake.lock | 97 | ||||
-rw-r--r-- | lib/nix-thin-edge-builder/flake.nix | 87 | ||||
-rw-r--r-- | lib/nix-thin-edge-builder/writeCargoToml.nix | 51 | ||||
-rw-r--r-- | lib/nix-thin-edge-builder/writeSrcMainRs.nix | 12 |
4 files changed, 247 insertions, 0 deletions
diff --git a/lib/nix-thin-edge-builder/flake.lock b/lib/nix-thin-edge-builder/flake.lock new file mode 100644 index 00000000..31cda59b --- /dev/null +++ b/lib/nix-thin-edge-builder/flake.lock @@ -0,0 +1,97 @@ +{ + "nodes": { + "crane": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1660447363, + "narHash": "sha256-Y1OtGWEb0bgFlp9YULaiYcgfEoHVIz1NuXwzlFZ/0HE=", + "owner": "ipetkov", + "repo": "crane", + "rev": "5548a68f5d4d60a2315ab1232e6fba5528e71dc8", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1650374568, + "narHash": "sha256-Z+s0J8/r907g149rllvwhb4pKi8Wam5ij0st8PwAh+E=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "b4a34015c698c7793d592d66adbab377907a2be8", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1656928814, + "narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "locked": { + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1661300288, + "narHash": "sha256-R3FTmbhGhJ4bZZYFn/7KZjoFemhuSYCjPdPLzYSJKpI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "eb569cf5cc4ff90eb78896c04ee1fd377acc7e1b", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/lib/nix-thin-edge-builder/flake.nix b/lib/nix-thin-edge-builder/flake.nix new file mode 100644 index 00000000..8ece62d3 --- /dev/null +++ b/lib/nix-thin-edge-builder/flake.nix @@ -0,0 +1,87 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + crane.url = "github:ipetkov/crane"; + crane.inputs.nixpkgs.follows = "nixpkgs"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = inputs@{ self, nixpkgs, flake-utils, crane, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + craneLib = crane.lib.${system}; + pkgs = import nixpkgs { inherit system; }; + in + { + lib = rec { + officialComponents = [ + (mkThinEdgeComponent { src = ../../plugins/plugin_avg; pluginBuilderPath = "foo"; }) + ]; + + mkThinEdgeComponent = attrs@{ + name ? null, + version ? null, + src, + pluginBuilderPath, + }: + let + crateInfo = craneLib.crateNameFromCargoToml { + cargoToml = "${src}/Cargo.toml"; + }; + in { + name = if builtins.isNull name + then crateInfo.pname + else name; + + version = if builtins.isNull version + then crateInfo.version + else version; + + inherit src pluginBuilderPath; + }; + + mkThinEdgeCrateSource = attrs@{ + defaultComponents ? officialComponents, + extraComponents ? [], + }: + let + allComponents = defaultComponents ++ extraComponents; + + writeCargoToml = import ./writeCargoToml.nix; + writeSrcMainRs = import ./writeSrcMainRs.nix; + + cargoToml = writeCargoToml { + inherit craneLib; + components = allComponents; + }; + + srcmainrs = writeSrcMainRs { + inherit pkgs craneLib; + components = allComponents; + }; + + thinEdgeCrateSrc = pkgs.runCommand "buildThinEdgeCrateSrc" {} '' + mkdir -p $out/src + cp -v "${cargoToml}" $out/Cargo.toml + cp -v "${srcmainrs}" $out/src/main.rs + ''; + in thinEdgeCrateSrc; + + mkThinEdge = attrs@{ + defaultComponents ? officialComponents, + extraComponents ? [], + }: + let + thinEdgeCrateSrc = mkThinEdgeCrateSource { + inherit defaultComponents extraComponents; + }; + + builtPackage = craneLib.buildPackage { + pname = "thin-edge"; + version = "0.1.0"; + src = thinEdgeCrateSrc; + }; + in builtPackage; + }; + }); +} diff --git a/lib/nix-thin-edge-builder/writeCargoToml.nix b/lib/nix-thin-edge-builder/writeCargoToml.nix new file mode 100644 index 00000000..34a276af --- /dev/null +++ b/lib/nix-thin-edge-builder/writeCargoToml.nix @@ -0,0 +1,51 @@ +{ craneLib, components }: + +let + edition = "2021"; + + defaultDependencies = { + clap = { version = "3"; features = ["derive" "cargo" "suggestions"]; }; + toml = "0.5.8"; + tokio = { version = "1"; features = ["fs" "macros" "rt-multi-thread" "signal"]; }; + miette = { version = "4.7"; features = ["fancy"]; }; + cfg-if = "1"; + tracing = "0.1"; + tracing-subscriber = { version = "0.3.11"; features = ["env-filter"]; }; + tracing-chrome = "0.6"; + tracing-tracy = "0.9"; + cfg_table = "1.0.0"; + nu-ansi-term = "0.45.1"; + pretty = { version = "0.11.3"; features = ["termcolor"]; }; + termcolor = "1.1.3"; + termimad = "0.20.1"; + term_size = "0.3.2"; + owo-colors = "3.4.0"; + textwrap = "0.15.0"; + + tedge_api = { path = "../crates/core/tedge_api"; }; + tedge_core = { path = "../crates/core/tedge_core"; }; + tedge_lib = { path = "../crates/core/tedge_lib"; }; + + env_logger = { version = "0.9"; optional = true; }; + }; + + componentDependencies = + let + mkDep = component: { + name = component.name; + value = { + path = component.src; + }; + }; + in builtins.listToAttrs (builtins.map mkDep components); +in + +craneLib.writeTOML "Cargo.toml" { + package = { + name = "thin-edge"; + version = "0.1.0"; + edition = "2021"; + }; + + dependencies = defaultDependencies // componentDependencies; +} diff --git a/lib/nix-thin-edge-builder/writeSrcMainRs.nix b/lib/nix-thin-edge-builder/writeSrcMainRs.nix new file mode 100644 index 00000000..b95bca48 --- /dev/null +++ b/lib/nix-thin-edge-builder/writeSrcMainRs.nix @@ -0,0 +1,12 @@ +{ pkgs, craneLib, components }: + +let + foo = 1; +in +pkgs.writeText "main.rs" '' +/// This file was generated by nix + +fn main() { + println!("HelloWorld"); +} +'' |