@@ -160,18 +160,9 @@ func Debroot(ctx context.Context, sOpt dalec.SourceOpts, spec *dalec.Spec, worke
160160 states = append (states , dalecDir .File (llb .Mkfile (filepath .Join (dir , "dalec/" + customSystemdPostinstFile ), 0o600 , customEnable ), opts ... ))
161161 }
162162
163- postinst := bytes .NewBuffer (nil )
164- artifacts := spec .GetArtifacts (target )
165- writeUsersPostInst (postinst , artifacts .Users )
166- writeGroupsPostInst (postinst , artifacts .Groups )
167- setArtifactOwnershipPostInst (postinst , spec , target )
168- setArtifactCapabilitiesPostInst (postinst , spec , target )
169-
170- if postinst .Len () > 0 {
171- dt := []byte ("#!/usr/bin/env sh\n set -e\n " )
172- dt = append (dt , postinst .Bytes ()... )
173-
174- states = append (states , dalecDir .File (llb .Mkfile (filepath .Join (dir , "postinst" ), 0o700 , dt ), opts ... ))
163+ postinst := generatePostinst (spec , target )
164+ if len (postinst ) > 0 {
165+ states = append (states , dalecDir .File (llb .Mkfile (filepath .Join (dir , "postinst" ), 0o700 , postinst ), opts ... ))
175166 }
176167
177168 patchDir := dalecDir .File (llb .Mkdir (filepath .Join (dir , "dalec/patches" ), 0o755 ), opts ... )
@@ -181,6 +172,7 @@ func Debroot(ctx context.Context, sOpt dalec.SourceOpts, spec *dalec.Spec, worke
181172 states = append (states , pls ... )
182173 }
183174
175+ artifacts := spec .GetArtifacts (target )
184176 if len (artifacts .Links ) > 0 {
185177 buf := bytes .NewBuffer (nil )
186178
@@ -196,6 +188,28 @@ func Debroot(ctx context.Context, sOpt dalec.SourceOpts, spec *dalec.Spec, worke
196188 return dalec .MergeAtPath (in , states , "/" )
197189}
198190
191+ func generatePostinst (spec * dalec.Spec , target string ) []byte {
192+ buf := bytes .NewBuffer (nil )
193+
194+ artifacts := spec .GetArtifacts (target )
195+ writeUsersPostInst (buf , artifacts .Users )
196+ writeGroupsPostInst (buf , artifacts .Groups )
197+ setArtifactOwnershipPostInst (buf , spec , target )
198+ setArtifactCapabilitiesPostInst (buf , spec , target )
199+
200+ if buf .Len () == 0 {
201+ return nil
202+ }
203+
204+ // NOTE: `#DEBHELPER#` is a special marker that will be replaced by
205+ // debhelper when it generates the final postinst script.
206+ // Very important that this is included.
207+ dt := []byte ("#!/usr/bin/env sh\n set -e\n \n #DEBHELPER#\n \n " )
208+ dt = append (dt , buf .Bytes ()... )
209+
210+ return dt
211+ }
212+
199213func fixupArtifactPerms (spec * dalec.Spec , target string , cfg * SourcePkgConfig ) []byte {
200214 buf := bytes .NewBuffer (nil )
201215 writeScriptHeader (buf , cfg )
0 commit comments