2018-02-28 21:02:21 +01:00
|
|
|
let {
|
|
|
|
|
|
|
|
body = lib;
|
|
|
|
|
|
|
|
lib = nixpkgs.lib // builtins // {
|
|
|
|
|
|
|
|
evalSource = let
|
|
|
|
eval = source: lib.evalModules {
|
|
|
|
modules = lib.singleton {
|
|
|
|
_file = toString ./.;
|
|
|
|
imports = map (source: { inherit source; }) (lib.toList source);
|
|
|
|
options.source = lib.mkOption {
|
|
|
|
default = {};
|
|
|
|
type = lib.types.attrsOf lib.types.source;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
sanitize = x: lib.getAttr (lib.typeOf x) {
|
2018-11-28 08:34:19 +01:00
|
|
|
bool = x;
|
2018-10-31 18:24:57 +01:00
|
|
|
list = map sanitize x;
|
2018-02-28 21:02:21 +01:00
|
|
|
set = lib.mapAttrs
|
|
|
|
(lib.const sanitize)
|
|
|
|
(lib.filterAttrs
|
|
|
|
(name: value: name != "_module" && value != null) x);
|
|
|
|
string = x;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
# This function's return value can be used as pkgs.populate input.
|
|
|
|
source: sanitize (eval source).config.source;
|
|
|
|
|
2021-01-16 14:12:45 +01:00
|
|
|
maybeHostName = default: let
|
2018-02-28 21:02:21 +01:00
|
|
|
# We're parsing /etc/hostname here because reading
|
|
|
|
# /proc/sys/kernel/hostname yields ""
|
2021-01-16 14:12:45 +01:00
|
|
|
path = "/etc/hostname";
|
|
|
|
lines = lib.splitString "\n" (lib.readFile path);
|
|
|
|
hostNames = lib.filter lib.types.label.check lines;
|
2018-02-28 21:02:21 +01:00
|
|
|
in
|
2021-01-16 14:12:45 +01:00
|
|
|
if lib.pathExists path then
|
|
|
|
if lib.length hostNames == 1 then
|
|
|
|
lib.head hostNames
|
|
|
|
else
|
|
|
|
lib.trace "malformed ${path}" default
|
|
|
|
else
|
|
|
|
default;
|
2018-02-28 21:02:21 +01:00
|
|
|
|
2020-06-08 22:58:11 +02:00
|
|
|
firstWord = s:
|
|
|
|
lib.head (lib.match "^([^[:space:]]*).*" s);
|
|
|
|
|
2018-07-12 14:14:27 +02:00
|
|
|
isLocalTarget = let
|
|
|
|
origin = lib.mkTarget "";
|
|
|
|
in target:
|
|
|
|
target.user == origin.user &&
|
|
|
|
lib.elem target.host [origin.host "localhost"];
|
|
|
|
|
2018-02-28 21:02:21 +01:00
|
|
|
mkTarget = s: let
|
2019-11-29 00:01:37 +01:00
|
|
|
parse = lib.match "(([^@]*)@)?(([^:/]+))?(:([^/]+))?(/.*)?" s;
|
2018-02-28 21:02:21 +01:00
|
|
|
elemAt' = xs: i: if lib.length xs > i then lib.elemAt xs i else null;
|
2020-04-18 23:36:53 +02:00
|
|
|
filterNull = lib.filterAttrs (n: v: v != null);
|
|
|
|
in {
|
|
|
|
user = lib.getEnv "LOGNAME";
|
2021-01-16 14:12:45 +01:00
|
|
|
host = lib.maybeEnv "HOSTNAME" (lib.maybeHostName "localhost");
|
2020-04-18 23:36:53 +02:00
|
|
|
port = "22";
|
|
|
|
path = "/var/src";
|
2019-11-29 12:34:31 +01:00
|
|
|
sudo = false;
|
2020-04-18 23:05:18 +02:00
|
|
|
extraOptions = [];
|
2020-04-18 23:36:53 +02:00
|
|
|
} // (if lib.isString s then filterNull {
|
|
|
|
user = elemAt' parse 1;
|
|
|
|
host = elemAt' parse 3;
|
|
|
|
port = elemAt' parse 5;
|
|
|
|
path = elemAt' parse 6;
|
|
|
|
} else s);
|
2018-02-28 21:02:21 +01:00
|
|
|
|
2018-07-12 14:14:47 +02:00
|
|
|
shell = let
|
|
|
|
isSafeChar = lib.testString "[-+./0-9:=A-Z_a-z]";
|
|
|
|
quoteChar = c:
|
|
|
|
if isSafeChar c then c
|
|
|
|
else if c == "\n" then "'\n'"
|
|
|
|
else "\\${c}";
|
|
|
|
in {
|
|
|
|
quote = x: if x == "" then "''" else lib.stringAsChars quoteChar x;
|
|
|
|
};
|
|
|
|
|
2018-02-28 21:02:21 +01:00
|
|
|
test = re: x: lib.isString x && lib.testString re x;
|
|
|
|
testString = re: x: lib.match re x != null;
|
|
|
|
|
|
|
|
types = nixpkgs.lib.types // import ./types { lib = body; };
|
|
|
|
};
|
|
|
|
|
|
|
|
nixpkgs.lib = import <nixpkgs/lib>;
|
|
|
|
|
|
|
|
}
|