diff options
author | Ben Sima <ben@bsima.me> | 2023-08-17 21:21:07 -0400 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2023-08-17 22:44:04 -0400 |
commit | aac50fb4a0eea25a057bb2d7ebe80961c542a2a5 (patch) | |
tree | b6e6a26cd7dec9937003aa33ec6f0875be2ee99d /Biz/Bild | |
parent | cc6aac612e36da3c9b9b4e47fc16ed512a79f2d9 (diff) |
Nixify C build
This is working with libsodium as an example. Its unfortunate that we need the
extra ':arg -lsodium' but how else can I get the name of the library for
linking? Is that something in the nix attr metadata? Anyway, an optimization for
another day.
Diffstat (limited to 'Biz/Bild')
-rw-r--r-- | Biz/Bild/Builder.nix | 20 | ||||
-rw-r--r-- | Biz/Bild/Example.c | 15 |
2 files changed, 29 insertions, 6 deletions
diff --git a/Biz/Bild/Builder.nix b/Biz/Bild/Builder.nix index 7ce29a2..2b62b89 100644 --- a/Biz/Bild/Builder.nix +++ b/Biz/Bild/Builder.nix @@ -19,6 +19,8 @@ with builtins; let srcs_ = (lib.strings.splitString " " srcs) ++ [main]; + isEmpty = x: x == null || x == []; + skip = ["_" ".direnv"]; filter = file: type: if elem (baseNameOf file) skip then false @@ -35,12 +37,12 @@ let src = lib.sources.cleanSourceWith {inherit filter; src = lib.sources.cleanSource root;}; langdeps_ = - if langdeps == null || langdeps == [] then + if isEmpty langdeps then [] else private.selectAttrs (lib.strings.splitString " " langdeps) private.${packageSet}; sysdeps_ = - if sysdeps == null || sysdeps == [] then + if isEmpty sysdeps then [] else private.selectAttrs (lib.strings.splitString " " sysdeps) private.nixpkgs.pkgs; @@ -53,7 +55,6 @@ in { buildPhase = compileLine; }; - haskell = stdenv.mkDerivation rec { inherit name src BIZ_ROOT postUnpack; buildInputs = sysdeps_ ++ [ @@ -65,6 +66,19 @@ in { buildPhase = compileLine; }; + c = stdenv.mkDerivation rec { + inherit name src BIZ_ROOT postUnpack; + buildInputs = langdeps_ ++ sysdeps_; + installPhase = "install -D ${name} $out/bin/${name}"; + buildPhase = lib.strings.concatStringsSep " " [ + compileLine + (if isEmpty langdeps then "" else + "$(pkg-config --cflags ${langdeps})") + (if isEmpty sysdeps then "" else + "$(pkg-config --libs ${sysdeps})") + ]; + }; + python = buildPythonApplication rec { inherit name src BIZ_ROOT postUnpack; propagatedBuildInputs = [ (private.pythonWith (_: langdeps_)) ] ++ sysdeps_; diff --git a/Biz/Bild/Example.c b/Biz/Bild/Example.c index 9966dba..52ea9b9 100644 --- a/Biz/Bild/Example.c +++ b/Biz/Bild/Example.c @@ -1,6 +1,15 @@ -// : out helloworld.exe -void -main () +// : out examplesodium.exe +// : dep libsodium +// : arg -lsodium +#include <sodium.h> + +int +main (void) { + if (sodium_init () < 0) + { + /* panic! the library couldn't be initialized; it is not safe to use */ + } printf ("Biz/Bild/Example.c: Hello world!\n"); + return 0; } |