-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
145 lines (134 loc) · 4.65 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{
inputs = {
devshell = {
url = "github:numtide/devshell";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-parts.url = "github:hercules-ci/flake-parts";
nci = {
url = "github:yusdacra/nix-cargo-integration";
inputs.nixpkgs.follows = "nixpkgs";
};
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
pre-commit-hooks = {
url = "github:cachix/pre-commit-hooks.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
inputs:
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
inputs.devshell.flakeModule
inputs.flake-parts.flakeModules.easyOverlay
inputs.nci.flakeModule
inputs.pre-commit-hooks.flakeModule
inputs.treefmt-nix.flakeModule
];
systems = [
"x86_64-linux"
"aarch64-linux"
];
flake =
{ config, ... }:
{
nixosModules.default = {
imports = [ ./nix/nixosModules/elewrap.nix ];
nixpkgs.overlays = [ config.overlays.default ];
};
};
perSystem =
{
config,
lib,
pkgs,
...
}:
let
projectName = "elewrap";
# Generates the necessary environment variables to allow building
# elewrap for the given command and options.
mkElewrapEnvironment =
{
targetUser,
command,
commandDelimiter ? "\t",
allowedUsers ? [ ],
allowedGroups ? [ ],
passEnvironment ? [ ],
passArguments ? false,
verifySha512 ? true,
...
}:
{
# XXX: assert commandDelimiter not in any element of command
ELEWRAP_TARGET_USER = targetUser;
ELEWRAP_TARGET_COMMAND = lib.concatStringsSep commandDelimiter command;
ELEWRAP_TARGET_COMMAND_DELIMITER = commandDelimiter;
ELEWRAP_PASS_ARGUMENTS = toString passArguments;
}
// lib.optionalAttrs (passEnvironment != [ ]) {
ELEWRAP_PASS_ENVIRONMENT = lib.concatStringsSep "," passEnvironment;
}
// lib.optionalAttrs verifySha512 {
ELEWRAP_TARGET_COMMAND_SHA512 = builtins.hashFile "sha512" (lib.head command);
}
// lib.optionalAttrs (allowedUsers != [ ]) {
ELEWRAP_ALLOWED_USERS = lib.concatStringsSep "," allowedUsers;
}
// lib.optionalAttrs (allowedGroups != [ ]) {
ELEWRAP_ALLOWED_GROUPS = lib.concatStringsSep "," allowedGroups;
};
# A dummy environment that can be used to test compilation
# and to have a functioning development setup.
dummyElewrapEnvironment = mkElewrapEnvironment {
targetUser = "root";
command = [ "${pkgs.coreutils}/bin/id" ];
};
in
{
devshells.default = {
packages = [
config.treefmt.build.wrapper
pkgs.cargo-release
];
env = lib.mapAttrsToList (name: value: { inherit name value; }) dummyElewrapEnvironment;
devshell.startup.pre-commit.text = config.pre-commit.installationScript;
};
pre-commit.settings.hooks.treefmt.enable = true;
treefmt = {
projectRootFile = "flake.nix";
programs = {
deadnix.enable = true;
statix.enable = true;
nixfmt.enable = true;
rustfmt.enable = true;
};
};
nci.projects.${projectName} = {
path = ./.;
numtideDevshell = "default";
};
nci.crates.${projectName} = {
# Wrapper function that generates a elewrap binary for the given settings.
# Refer to mkElewrapEnvironment for available settings.
drvConfig.public.mkElewrap =
elewrapConfig:
config.nci.outputs.${projectName}.packages.release.out.overrideAttrs (prev: {
pname = prev.pname + lib.optionalString (elewrapConfig ? name) "-${elewrapConfig.name}";
env = mkElewrapEnvironment elewrapConfig;
});
};
packages.default = config.nci.outputs.${projectName}.packages.release;
packages.nixosTest = import ./nix/tests/elewrap.nix {
inherit (inputs) self;
inherit pkgs lib;
};
overlayAttrs.elewrap = config.packages.default;
};
};
}