summary refs log tree commit diff
path: root/pkgs/build-support/writers
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/build-support/writers')
-rw-r--r--pkgs/build-support/writers/default.nix59
-rw-r--r--pkgs/build-support/writers/test.nix82
2 files changed, 80 insertions, 61 deletions
diff --git a/pkgs/build-support/writers/default.nix b/pkgs/build-support/writers/default.nix
index 4673b4e6cd8..47919c251af 100644
--- a/pkgs/build-support/writers/default.nix
+++ b/pkgs/build-support/writers/default.nix
@@ -1,4 +1,4 @@
-{ pkgs, lib }:
+{ pkgs, lib, gawk, gnused, gixy }:
 
 with lib;
 rec {
@@ -63,7 +63,7 @@ rec {
   #
   # Examples:
   #   writeSimpleC = makeBinWriter { compileScript = name: "gcc -o $out $contentPath"; }
-  makeBinWriter = { compileScript }: nameOrPath: content:
+  makeBinWriter = { compileScript, strip ? true }: nameOrPath: content:
     assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null);
     assert lib.or (types.path.check content) (types.str.check content);
     let
@@ -76,6 +76,8 @@ rec {
       contentPath = content;
     }) ''
       ${compileScript}
+      ${lib.optionalString strip
+         "${pkgs.binutils-unwrapped}/bin/strip --strip-unneeded $out"}
       ${optionalString (types.path.check nameOrPath) ''
         mv $out tmp
         mkdir -p $out/$(dirname "${nameOrPath}")
@@ -109,7 +111,10 @@ rec {
   #        return 0;
   #      }
   #    ''
-  writeC = name: { libraries ? [] }:
+  writeC = name: {
+    libraries ? [],
+    strip ? true
+  }:
     makeBinWriter {
       compileScript = ''
         PATH=${makeBinPath [
@@ -117,7 +122,7 @@ rec {
           pkgs.coreutils
           pkgs.findutils
           pkgs.gcc
-          pkgs.pkgconfig
+          pkgs.pkg-config
         ]}
         export PKG_CONFIG_PATH=${concatMapStringsSep ":" (pkg: "${pkg}/lib/pkgconfig") libraries}
         gcc \
@@ -131,8 +136,8 @@ rec {
             -Wall \
             -x c \
             "$contentPath"
-        strip --strip-unneeded "$out"
       '';
+      inherit strip;
     } name;
 
   # writeCBin takes the same arguments as writeC but outputs a directory (like writeScriptBin)
@@ -164,21 +169,39 @@ rec {
   #   '';
   writeHaskell = name: {
     libraries ? [],
-    ghc ? pkgs.ghc
+    ghc ? pkgs.ghc,
+    ghcArgs ? [],
+    strip ? true
   }:
     makeBinWriter {
       compileScript = ''
         cp $contentPath tmp.hs
-        ${ghc.withPackages (_: libraries )}/bin/ghc tmp.hs
+        ${ghc.withPackages (_: libraries )}/bin/ghc ${lib.escapeShellArgs ghcArgs} tmp.hs
         mv tmp $out
-        ${pkgs.binutils-unwrapped}/bin/strip --strip-unneeded "$out"
       '';
+      inherit strip;
     } name;
 
   # writeHaskellBin takes the same arguments as writeHaskell but outputs a directory (like writeScriptBin)
   writeHaskellBin = name:
     writeHaskell "/bin/${name}";
 
+  writeRust = name: {
+      rustc ? pkgs.rustc,
+      rustcArgs ? [],
+      strip ? true
+  }:
+    makeBinWriter {
+      compileScript = ''
+        cp "$contentPath" tmp.rs
+        PATH=${makeBinPath [pkgs.gcc]} ${lib.getBin rustc}/bin/rustc ${lib.escapeShellArgs rustcArgs} -o "$out" tmp.rs
+      '';
+      inherit strip;
+    } name;
+
+  writeRustBin = name:
+    writeRust "/bin/${name}";
+
   # writeJS takes a name an attributeset with libraries and some JavaScript sourcecode and
   # returns an executable
   #
@@ -218,10 +241,11 @@ rec {
   writeNginxConfig = name: text: pkgs.runCommandLocal name {
     inherit text;
     passAsFile = [ "text" ];
+    nativeBuildInputs = [ gawk gnused gixy ];
   } /* sh */ ''
     # nginx-config-formatter has an error - https://github.com/1connect/nginx-config-formatter/issues/16
-    ${pkgs.gawk}/bin/awk -f ${awkFormatNginx} "$textPath" | ${pkgs.gnused}/bin/sed '/^\s*$/d' > $out
-    ${pkgs.gixy}/bin/gixy $out
+    awk -f ${awkFormatNginx} "$textPath" | sed '/^\s*$/d' > $out
+    gixy $out
   '';
 
   # writePerl takes a name an attributeset with libraries and some perl sourcecode and
@@ -233,18 +257,9 @@ rec {
   #     print "Howdy!\n" if true;
   #   ''
   writePerl = name: { libraries ? [] }:
-  let
-    perl-env = pkgs.buildEnv {
-      name = "perl-environment";
-      paths = libraries;
-      pathsToLink = [
-        "/${pkgs.perl.libPrefix}"
-      ];
-    };
-  in
-  makeScriptWriter {
-    interpreter = "${pkgs.perl}/bin/perl -I ${perl-env}/${pkgs.perl.libPrefix}";
-  } name;
+    makeScriptWriter {
+      interpreter = "${pkgs.perl.withPackages (p: libraries)}/bin/perl";
+    } name;
 
   # writePerlBin takes the same arguments as writePerl but outputs a directory (like writeScriptBin)
   writePerlBin = name:
diff --git a/pkgs/build-support/writers/test.nix b/pkgs/build-support/writers/test.nix
index d284bda43d0..00cad9a96b5 100644
--- a/pkgs/build-support/writers/test.nix
+++ b/pkgs/build-support/writers/test.nix
@@ -1,25 +1,23 @@
-{
-  glib,
-  haskellPackages,
-  lib,
-  nodePackages,
-  perlPackages,
-  python2Packages,
-  python3Packages,
-  runCommand,
-  stdenv,
-  writers,
-  writeText
+{ glib
+, haskellPackages
+, lib
+, nodePackages
+, perlPackages
+, python2Packages
+, python3Packages
+, runCommand
+, writers
+, writeText
 }:
 with writers;
 let
 
   bin = {
-    bash = writeBashBin "test_writers" ''
+    bash = writeBashBin "test-writers-bash-bin" ''
      if [[ "test" == "test" ]]; then echo "success"; fi
     '';
 
-    c = writeCBin "test_writers" { libraries = [ ]; } ''
+    c = writeCBin "test-writers-c" { libraries = [ ]; } ''
       #include <stdio.h>
       int main() {
         printf("success\n");
@@ -27,11 +25,17 @@ let
       }
     '';
 
-    dash = writeDashBin "test_writers" ''
+    dash = writeDashBin "test-writers-dash-bin" ''
      test '~' = '~' && echo 'success'
     '';
 
-    haskell = writeHaskellBin "test_writers" { libraries = [ haskellPackages.acme-default ]; } ''
+    rust = writeRustBin "test-writers-rust-bin" {} ''
+      fn main(){
+        println!("success")
+      }
+    '';
+
+    haskell = writeHaskellBin "test-writers-haskell-bin" { libraries = [ haskellPackages.acme-default ]; } ''
       import Data.Default
 
       int :: Int
@@ -43,7 +47,7 @@ let
         _ -> print "fail"
     '';
 
-    js = writeJSBin "test_writers" { libraries = [ nodePackages.semver ]; } ''
+    js = writeJSBin "test-writers-js-bin" { libraries = [ nodePackages.semver ]; } ''
       var semver = require('semver');
 
       if (semver.valid('1.2.3')) {
@@ -53,12 +57,12 @@ let
       }
     '';
 
-    perl = writePerlBin "test_writers" { libraries = [ perlPackages.boolean ]; } ''
+    perl = writePerlBin "test-writers-perl-bin" { libraries = [ perlPackages.boolean ]; } ''
       use boolean;
       print "success\n" if true;
     '';
 
-    python2 = writePython2Bin "test_writers" { libraries = [ python2Packages.enum ]; } ''
+    python2 = writePython2Bin "test-writers-python2-bin" { libraries = [ python2Packages.enum ]; } ''
       from enum import Enum
 
 
@@ -69,7 +73,7 @@ let
       print Test.a
     '';
 
-    python3 = writePython3Bin "test_writers" { libraries = [ python3Packages.pyyaml ]; } ''
+    python3 = writePython3Bin "test-writers-python3-bin" { libraries = [ python3Packages.pyyaml ]; } ''
       import yaml
 
       y = yaml.load("""
@@ -80,11 +84,11 @@ let
   };
 
   simple = {
-    bash = writeBash "test_bash" ''
+    bash = writeBash "test-writers-bash" ''
      if [[ "test" == "test" ]]; then echo "success"; fi
     '';
 
-    c = writeC "test_c" { libraries = [ glib.dev ]; } ''
+    c = writeC "test-writers-c" { libraries = [ glib.dev ]; } ''
       #include <gio/gio.h>
       #include <stdio.h>
       int main() {
@@ -102,11 +106,11 @@ let
       }
     '';
 
-    dash = writeDash "test_dash" ''
+    dash = writeDash "test-writers-dash" ''
      test '~' = '~' && echo 'success'
     '';
 
-    haskell = writeHaskell "test_haskell" { libraries = [ haskellPackages.acme-default ]; } ''
+    haskell = writeHaskell "test-writers-haskell" { libraries = [ haskellPackages.acme-default ]; } ''
       import Data.Default
 
       int :: Int
@@ -118,7 +122,7 @@ let
         _ -> print "fail"
     '';
 
-    js = writeJS "test_js" { libraries = [ nodePackages.semver ]; } ''
+    js = writeJS "test-writers-js" { libraries = [ nodePackages.semver ]; } ''
       var semver = require('semver');
 
       if (semver.valid('1.2.3')) {
@@ -128,12 +132,12 @@ let
       }
     '';
 
-    perl = writePerl "test_perl" { libraries = [ perlPackages.boolean ]; } ''
+    perl = writePerl "test-writers-perl" { libraries = [ perlPackages.boolean ]; } ''
       use boolean;
       print "success\n" if true;
     '';
 
-    python2 = writePython2 "test_python2" { libraries = [ python2Packages.enum ]; } ''
+    python2 = writePython2 "test-writers-python2" { libraries = [ python2Packages.enum ]; } ''
       from enum import Enum
 
 
@@ -144,7 +148,7 @@ let
       print Test.a
     '';
 
-    python3 = writePython3 "test_python3" { libraries = [ python3Packages.pyyaml ]; } ''
+    python3 = writePython3 "test-writers-python3" { libraries = [ python3Packages.pyyaml ]; } ''
       import yaml
 
       y = yaml.load("""
@@ -153,21 +157,21 @@ let
       print(y[0]['test'])
     '';
 
-    python2NoLibs = writePython2 "test_python2_no_libs" {} ''
+    python2NoLibs = writePython2 "test-writers-python2-no-libs" {} ''
       print("success")
     '';
 
-    python3NoLibs = writePython3 "test_python3_no_libs" {} ''
+    python3NoLibs = writePython3 "test-writers-python3-no-libs" {} ''
       print("success")
     '';
   };
 
 
   path = {
-    bash = writeBash "test_bash" (writeText "test" ''
+    bash = writeBash "test-writers-bash-path" (writeText "test" ''
       if [[ "test" == "test" ]]; then echo "success"; fi
     '');
-    haskell = writeHaskell "test_haskell" { libraries = [ haskellPackages.acme-default ]; } (writeText "test" ''
+    haskell = writeHaskell "test-writers-haskell-path" { libraries = [ haskellPackages.acme-default ]; } (writeText "test" ''
       import Data.Default
 
       int :: Int
@@ -180,8 +184,8 @@ let
     '');
   };
 
-  writeTest = expectedValue: test:
-    writeDash "test-writers" ''
+  writeTest = expectedValue: name: test:
+    writeDash "run-${name}" ''
       if test "$(${test})" != "${expectedValue}"; then
         echo 'test ${test} failed'
         exit 1
@@ -189,12 +193,12 @@ let
     '';
 
 in runCommand "test-writers" {
-  passthru = { inherit writeTest bin simple; };
-  meta.platforms = stdenv.lib.platforms.all;
+  passthru = { inherit writeTest bin simple path; };
+  meta.platforms = lib.platforms.all;
 } ''
-  ${lib.concatMapStringsSep "\n" (test: writeTest "success" "${test}/bin/test_writers") (lib.attrValues bin)}
-  ${lib.concatMapStringsSep "\n" (test: writeTest "success" test) (lib.attrValues simple)}
-  ${lib.concatMapStringsSep "\n" (test: writeTest "success" test) (lib.attrValues path)}
+  ${lib.concatMapStringsSep "\n" (test: writeTest "success" test.name "${test}/bin/${test.name}") (lib.attrValues bin)}
+  ${lib.concatMapStringsSep "\n" (test: writeTest "success" test.name test) (lib.attrValues simple)}
+  ${lib.concatMapStringsSep "\n" (test: writeTest "success" test.name test) (lib.attrValues path)}
 
   echo 'nix-writers successfully tested' >&2
   touch $out