Skip to content

Commit

Permalink
Cache Apt and pacman package downloads with BuildKit cache mounts
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterDA committed Dec 9, 2024
1 parent 4a26fcc commit 71b186f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------
Expand Down
28 changes: 23 additions & 5 deletions src-opam/linux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 () =
Expand Down Expand Up @@ -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 () =
Expand Down
10 changes: 6 additions & 4 deletions src-opam/linux.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 71b186f

Please sign in to comment.