From 71b186f31b793a34757cc0c0f376efdf3f8b8ee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= Date: Wed, 6 Nov 2024 10:54:44 +0100 Subject: [PATCH] Cache Apt and pacman package downloads with BuildKit cache mounts --- CHANGES.md | 3 +++ src-opam/linux.ml | 28 +++++++++++++++++++++++----- src-opam/linux.mli | 10 ++++++---- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ef98636..1c2b252 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,9 @@ unreleased ---------- - Add Alpine 3.21, deprecate Alpine 3.20. (@MisterDA, #225) +- Cache packages downloads for Apt (Debian, Ubuntu) and pacman (Arch + Linux) based distributions using BuildKit cache mounts. + (@MisterDA, #224) v8.2.4 2024-11-18 ----------------- diff --git a/src-opam/linux.ml b/src-opam/linux.ml index 8805b39..56874a5 100644 --- a/src-opam/linux.ml +++ b/src-opam/linux.ml @@ -86,14 +86,30 @@ end (** Debian rules *) module Apt = struct + (* https://docs.docker.com/reference/dockerfile/#example-cache-apt-packages *) + let cache_mounts = + [ + mount_cache ~target:"/var/cache/apt" ~sharing:`Locked (); + mount_cache ~target:"/var/lib/apt" ~sharing:`Locked (); + ] + let update = - run "apt-get -y update" - @@ run "DEBIAN_FRONTEND=noninteractive apt-get -y upgrade" + run + {|mv /etc/apt/apt.conf.d/docker-clean /tmp/; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache|} + @@ run ~mounts:cache_mounts + "apt update && DEBIAN_FRONTEND=noninteractive apt-get -y upgrade" + @@ run + "mv /tmp/docker-clean /etc/apt/apt.conf.d/ && rm -f \ + /etc/apt/apt.conf.d/keep-cache" let install fmt = ksprintf (fun s -> - update @@ run "DEBIAN_FRONTEND=noninteractive apt-get -y install %s" s) + update + @@ run ~mounts:cache_mounts + "DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends \ + install -y %s" + s) fmt let dev_packages ?extra () = @@ -242,11 +258,13 @@ end (** Pacman rules *) module Pacman = struct - let update = run "pacman -Syu --noconfirm && yes | pacman -Scc" + let cache_mount = mount_cache ~target:"/var/cache/pacman" ~sharing:`Locked () + let update = run ~mounts:[ cache_mount ] "pacman -Syu --noconfirm --needed" let install fmt = ksprintf - (fun s -> run "pacman -Syu --noconfirm %s && yes | pacman -Scc" s) + (fun s -> + run ~mounts:[ cache_mount ] "pacman -Syu --noconfirm --needed %s" s) fmt let dev_packages ?extra () = diff --git a/src-opam/linux.mli b/src-opam/linux.mli index 026d537..ab84503 100644 --- a/src-opam/linux.mli +++ b/src-opam/linux.mli @@ -62,11 +62,12 @@ end module Apt : sig val update : t (** [update] will run [apt-get update && apt-get upgrade] non-interactively. - *) + Requires [syntax=docker/dockerfile:1]. *) val install : ('a, unit, string, t) format4 -> 'a (** [install fmt] will [apt-get update && apt-get install] the packages - specified by the [fmt] format string. *) + specified by the [fmt] format string. Requires + [syntax=docker/dockerfile:1]. *) val add_user : ?uid:int -> ?gid:int -> ?sudo:bool -> string -> t (** [add_user username] will install a new user with name [username] and a @@ -150,11 +151,12 @@ end (** Rules for Pacman-based distributions such as Archlinux *) module Pacman : sig val update : t - (** [update] will run [pacman -Syu] non-interactively. *) + (** [update] will run [pacman -Syu] non-interactively. Requires + [syntax=docker/dockerfile:1]. *) val install : ('a, unit, string, t) format4 -> 'a (** [install fmt] will [pacman -Syu] the packages specified by the [fmt] - format string. *) + format string. Requires [syntax=docker/dockerfile:1]. *) val add_user : ?uid:int -> ?gid:int -> ?sudo:bool -> string -> t (** [add_user username] will install a new user with name [username] and a