| 
 | 1 | +"""BuildPack for guix environments"""  | 
 | 2 | +import os  | 
 | 3 | + | 
 | 4 | +from ..base import BuildPack, BaseImage  | 
 | 5 | + | 
 | 6 | + | 
 | 7 | +class GuixBuildPack(BaseImage):  | 
 | 8 | +    """A Guix Package Manager BuildPack"""  | 
 | 9 | + | 
 | 10 | +    def get_path(self):  | 
 | 11 | +        """Return paths to be added to PATH environment variable"""  | 
 | 12 | +        return super().get_path() + [  | 
 | 13 | +            "/var/guix/profiles/per-user/${NB_USER}/.guix-profile/bin"  | 
 | 14 | +        ]  | 
 | 15 | + | 
 | 16 | + | 
 | 17 | +    def get_build_scripts(self):  | 
 | 18 | +        """  | 
 | 19 | +        Install a pinned version of Guix package manager,  | 
 | 20 | +        precised in guix-install.bash.  | 
 | 21 | +        """  | 
 | 22 | +        return super().get_build_scripts() + [  | 
 | 23 | +            (  | 
 | 24 | +                "root",  | 
 | 25 | +                """  | 
 | 26 | +                bash /tmp/.local/bin/guix-install.bash  | 
 | 27 | +                """,  | 
 | 28 | +            ),  | 
 | 29 | + | 
 | 30 | +        ]  | 
 | 31 | + | 
 | 32 | +    def get_build_script_files(self):  | 
 | 33 | + | 
 | 34 | +        """Copying guix installation script on the image"""  | 
 | 35 | +        return {  | 
 | 36 | +            "guix/guix-install.bash":  | 
 | 37 | +                "/tmp/.local/bin/guix-install.bash",  | 
 | 38 | +        }  | 
 | 39 | + | 
 | 40 | +    def get_assemble_scripts(self):  | 
 | 41 | +        """  | 
 | 42 | +        Launch Guix daemon with --disable-chroot to avoid the need  | 
 | 43 | +        of root privileges for the user.  | 
 | 44 | +        Make sure we never use Debian's python by error by renaming it  | 
 | 45 | +        then, as an user install packages listed in manifest.scm,  | 
 | 46 | +        use guix time-machine if channels.scm file exists.  | 
 | 47 | +        Finally set guix environment variables.  | 
 | 48 | +        """  | 
 | 49 | +        assemble_script ="""  | 
 | 50 | +                 /var/guix/profiles/per-user/root/current-guix/bin/guix-daemon \  | 
 | 51 | +                 --build-users-group=guixbuild --disable-chroot & \  | 
 | 52 | +                 mv /usr/bin/python /usr/bin/python.debian && \  | 
 | 53 | +                 su - $NB_USER -c '{}' && \  | 
 | 54 | +                 echo 'GUIX_PROFILE="/var/guix/profiles/per-user/$NB_USER/.guix-profile" ; \  | 
 | 55 | +                 source "$GUIX_PROFILE/etc/profile"'>> /etc/profile.d/99-guix.sh  | 
 | 56 | +                 """  | 
 | 57 | + | 
 | 58 | +        if os.path.exists(self.binder_path("channels.scm")):  | 
 | 59 | +            assemble_script = assemble_script.format(  | 
 | 60 | +                "guix time-machine -C  " + self.binder_path("channels.scm") +  | 
 | 61 | +                " -- package -m "  + self.binder_path("manifest.scm")  | 
 | 62 | +            )  | 
 | 63 | +        else:  | 
 | 64 | +            assemble_script = assemble_script.format(  | 
 | 65 | +                "guix package -m " + self.binder_path("manifest.scm")  | 
 | 66 | +            )  | 
 | 67 | +        return super().get_assemble_scripts() + [  | 
 | 68 | +            ( "root",  | 
 | 69 | +              assemble_script,  | 
 | 70 | +             )  | 
 | 71 | +        ]  | 
 | 72 | + | 
 | 73 | +    def detect(self):  | 
 | 74 | +        """Check if current repo should be built with the guix BuildPack"""  | 
 | 75 | +        return os.path.exists(self.binder_path("manifest.scm"))  | 
 | 76 | +      | 
0 commit comments