Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
unix-derivation-builder-impl.hh
1#pragma once
2
10
11namespace nix {
12
24class UnixDerivationBuilderImpl : public DerivationBuilderImpl
25{
26private:
27 void anchor() override;
28
29protected:
30
31public:
32
33 using DerivationBuilderImpl::DerivationBuilderImpl;
34
38 void cleanupOnDestruction() noexcept
39 {
40 /* Careful: we should never ever throw an exception from a
41 noexcept function. */
42 try {
43 killChild();
44 } catch (...) {
46 }
47 try {
48 stopDaemon();
49 } catch (...) {
51 }
52 try {
53 cleanupBuild(false);
54 } catch (...) {
56 }
57 }
58
59protected:
60
65 std::filesystem::path topTmpDir;
66
71
72 typedef StringMap Environment;
73 Environment env;
74
79
80 static const std::filesystem::path homeDir;
81
86
90 std::thread daemonThread;
91
93 {
94 std::thread thread;
96 };
97
101 std::list<DaemonWorkerState> daemonWorkerThreads;
102
103 const StorePathSet & originalPaths() override
104 {
105 return inputPaths;
106 }
107
108 bool isAllowed(const StorePath & path) override
109 {
110 if (inputPaths.count(path))
111 return true;
112 auto state(state_.lock());
113 auto iter = state->addedPaths.find(path);
114 if (iter == state->addedPaths.end())
115 return false;
116 return iter->second.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
117 }
118
119 bool isAllowed(const DrvOutput & id) override
120 {
121 return state_.lock()->addedDrvOutputs.count(id);
122 }
123
124 bool isAllowed(const DerivedPath & req);
125
126 bool shouldModifySandbox() override
127 {
128 return !usingSubmitted;
129 }
130
131 void submitOutput(const SingleDerivedPath & path, const OutputName & output) override;
132
133 friend struct RestrictedStore;
134
138 virtual bool needsHashRewrite()
139 {
140 return true;
141 }
142
143public:
144
145 std::optional<Descriptor> startBuild() override;
146
147 BuilderExit unprepareBuild() override;
148
149 void cleanupBuild(bool force) override;
150
151protected:
152
156 virtual std::unique_ptr<UserLock> getBuildUser()
157 {
158 return acquireUserLock(settings.nixStateDir, localSettings, 1, false);
159 }
160
170 PathsInChroot getPathsInSandbox();
171
172 virtual void setBuildTmpDir()
173 {
175 }
176
180 virtual std::filesystem::path tmpDirInSandbox()
181 {
182 assert(!topTmpDir.empty());
183 return topTmpDir;
184 }
185
190 virtual void prepareUser()
191 {
192 killSandbox(false);
193 }
194
199 virtual void prepareSandbox();
200
201 virtual Strings getPreBuildHookArgs()
202 {
203 return Strings({store->printStorePath(drvPath)});
204 }
205
209 void openSlave();
210
215 virtual void startChild();
216
217#if NIX_WITH_AWS_AUTH
223 std::optional<AwsCredentials> preResolveAwsCredentials();
224#endif
225
226private:
227
231 void initEnv();
232
233protected:
234
239
240private:
241
245 void startDaemon();
246
251 void stopDaemon();
252
253protected:
254
255 void addDependencyImpl(const StorePath & path) override;
256
263 void chownToBuilder(const std::filesystem::path & path);
264
270 void chownToBuilder(int fd, const std::filesystem::path & path);
271
280 void writeBuilderFile(const OsFilename & name, std::string_view contents);
281
286 {
287#if NIX_WITH_AWS_AUTH
288 std::optional<AwsCredentials> awsCredentials;
289#endif
290 };
291
295 void runChild(RunChildArgs args);
296
301 virtual void enterChroot() {}
302
307 virtual void setUser();
308
313 virtual void execBuilder(const Strings & args, const Strings & envStrs);
314
315protected:
316
321 virtual void killSandbox(bool getStats);
322
323public:
324
325 bool killChild() override;
326
327private:
328
329 bool decideWhetherDiskFull();
330
336 StorePath makeFallbackPath(const StorePath & path);
337
345 StorePath makeFallbackPath(OutputNameView outputName);
346};
347
348} // namespace nix
Definition file-descriptor.hh:265
std::filesystem::path tmpDir
Definition derivation-builder-impl.hh:46
Definition path.hh:33
Definition unix-derivation-builder-impl.hh:25
virtual void execBuilder(const Strings &args, const Strings &envStrs)
Definition unix-derivation-builder.cc:974
virtual void startChild()
Definition unix-derivation-builder.cc:535
virtual bool needsHashRewrite()
Definition unix-derivation-builder-impl.hh:138
std::optional< Descriptor > startBuild() override
Definition unix-derivation-builder.cc:249
const StorePathSet & originalPaths() override
Definition unix-derivation-builder-impl.hh:103
virtual void setUser()
Definition unix-derivation-builder.cc:947
std::filesystem::path topTmpDir
Definition unix-derivation-builder-impl.hh:65
bool usingSubmitted
Definition unix-derivation-builder-impl.hh:78
virtual void enterChroot()
Definition unix-derivation-builder-impl.hh:301
void processSandboxSetupMessages()
Definition unix-derivation-builder.cc:549
void writeBuilderFile(const OsFilename &name, std::string_view contents)
Definition unix-derivation-builder.cc:832
void cleanupOnDestruction() noexcept
Definition unix-derivation-builder-impl.hh:38
virtual void killSandbox(bool getStats)
Definition unix-derivation-builder.cc:106
bool killChild() override
Definition unix-derivation-builder.cc:115
bool shouldModifySandbox() override
Definition unix-derivation-builder-impl.hh:126
virtual void prepareUser()
Definition unix-derivation-builder-impl.hh:190
void runChild(RunChildArgs args)
Definition unix-derivation-builder.cc:843
BuilderExit unprepareBuild() override
Definition unix-derivation-builder.cc:135
void openSlave()
Definition unix-derivation-builder.cc:487
virtual std::filesystem::path tmpDirInSandbox()
Definition unix-derivation-builder-impl.hh:180
void chownToBuilder(const std::filesystem::path &path)
Definition unix-derivation-builder.cc:817
std::thread daemonThread
Definition unix-derivation-builder-impl.hh:90
bool isAllowed(const StorePath &path) override
Definition unix-derivation-builder-impl.hh:108
AutoCloseFD tmpDirFd
Definition unix-derivation-builder-impl.hh:70
void addDependencyImpl(const StorePath &path) override
Definition unix-derivation-builder.cc:815
PathsInChroot getPathsInSandbox()
Definition unix-derivation-builder.cc:406
virtual std::unique_ptr< UserLock > getBuildUser()
Definition unix-derivation-builder-impl.hh:156
AutoCloseFD daemonSocket
Definition unix-derivation-builder-impl.hh:85
void submitOutput(const SingleDerivedPath &path, const OutputName &output) override
Definition unix-derivation-builder.cc:793
std::list< DaemonWorkerState > daemonWorkerThreads
Definition unix-derivation-builder-impl.hh:101
virtual void prepareSandbox()
Definition unix-derivation-builder.cc:481
void cleanupBuild(bool force) override
Definition unix-derivation-builder.cc:979
Definition ref.hh:58
std::string_view OutputNameView
Definition outputs-spec.hh:24
std::string OutputName
Definition outputs-spec.hh:18
Definition derivation-builder.hh:163
const StorePathSet & inputPaths
Definition derivation-builder.hh:101
Definition realisation.hh:26
Definition restricted-store.cc:47
Definition derived-path.hh:105
Definition unix-derivation-builder-impl.hh:93
Definition unix-derivation-builder-impl.hh:286
std::map< std::string, std::string, std::less<> > StringMap
Definition types.hh:26
std::unique_ptr< UserLock > acquireUserLock(const std::filesystem::path &stateDir, const LocalSettings &localSettings, uid_t nrIds, bool useUserNamespace)
Definition user-lock.cc:227
void ignoreExceptionInDestructor(Verbosity lvl=lvlError)
Definition util.cc:265